iOS技术专题

设计多控制器跳转管理模仿 UITabBarController

2016-04-03  本文已影响0人  YuePr

分析三种系统自带的方法实现ViewController跳转

[self.navigationController pushViewController:vc animated:YES];

-  出栈(弹出栈顶控制器) 

[self.navigationController popViewControllerAnimated:YES];

- 直接跳到栈底控制器(回到根控制器) 

[self.navigationController popToRootViewControllerAnimated:YES];

栈底为根视图控制器,用户在场景间切换时,依次将试图控制器压入栈中,且当前场景的试图控制器位于栈顶。要返回上一级,导航控制器将弹出栈顶的控制器,从而回到它下面的控制器。


- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion

MainVC *mainVC = [[MainVC alloc] init];

[self presentViewController:mainVC animated:YES completion:nil];

这种方式一般出现在需要使用者完成某件事情,如输入密码、增加资料等操作后,才能(回到跳转前的控制器)继续。例如系统的WIFI连接输入密码提示。默认动画是从下至上。

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated

这种方式一般是使用者浏览资料,继而可以前进到下一个页面或回到上一个页面。默认动画是从右至左。

- (void)addChildViewController:(UIViewController *)childController

分析完了系统的做法,下面是模仿系统UITabBarController进行多控制器管理的一个小案例:


- (IBAction)showScoiety:(UIButton *)sender {

    // 展示社会

    ScoietyViewController *scoietyVc = [[ScoietyViewController alloc] init];

    scoietyVc.view.backgroundColor = sender.backgroundColor;

    scoietyVc.view.frame = CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64);

    [self.view addSubview:scoietyVc.view];

}

- (IBAction)showTopLine:(UIButton *)sender {

    // 展示头条

    TopLineViewController *topLineVc = [[TopLineViewController alloc] init];

    topLineVc.view.backgroundColor = sender.backgroundColor;

    topLineVc.view.frame = CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64);

        [self.view addSubview:topLineVc.view];

}

- (IBAction)showHot:(UIButton *)sender {

    // 展示热点

    HotViewController *hotVc = [[HotViewController alloc] init];

    hotVc.view.backgroundColor = sender.backgroundColor;

    hotVc.view.frame = CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64);

    [self.view addSubview:hotVc.view];

}

subview添加新控制器.png

#import "ViewController.h"

#import "ScoietyViewController.h"

#import "TopLineViewController.h"

#import "HotViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIView *titleView;

// 存放子控制器的view

@property (weak, nonatomic) IBOutlet UIView *contentView;

@end

@implementation ViewController

// 点击标题

- (IBAction)clickTitle:(UIButton *)sender {

 // 移除其他控制器的view

    for (UIView *childView in self.contentView.subviews) {

        [childView removeFromSuperview];

    }

    // 获取对应子控制器

    UIViewController *vc = self.childViewControllers[sender.tag];

    vc.view.frame = self.contentView.bounds;

    vc.view.backgroundColor = sender.backgroundColor;

    // 1.把对应子控制器的view添加上去

    [self.contentView addSubview:vc.view];



}

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    // 1. 添加所有的子控制器

    [self setupAllChildViewController];

    // 2.设置子控制器对应标题

    [self setupAllTitleButton];  

}

#pragma mark - 设置所有子控制器对应标题

- (void)setupAllTitleButton

{

    NSInteger count = self.titleView.subviews.count;

      for (int i = 0; i < count; i++) {

       UIButton *btn = self.titleView.subviews[i];

      UIViewController *vc = self.childViewControllers[i];

        [btn setTitle:vc.title forState:UIControlStateNormal];

    }

}

#pragma mark - 添加所有的子控制器

- (void)setupAllChildViewController

{

    // 社会

    ScoietyViewController *vc1 = [[ScoietyViewController alloc] init];

    vc1.title = @"社会";

    [self addChildViewController:vc1];   

    // 头条

    TopLineViewController *vc2 = [[TopLineViewController alloc] init];

    vc2.title = @"头条";

    [self addChildViewController:vc2];   

    // 热点

    HotViewController *vc3 = [[HotViewController alloc] init];

    vc3.title = @"热点";

    [self addChildViewController:vc3];    

}

@end

父子控制器.png
上一篇下一篇

猜你喜欢

热点阅读