父子控制器的简单实现

创建导航栏是 UISegmentedControl的控制器,通过点击切换控制器。

父子控制器的简单实现_第1张图片
第一个控制器
父子控制器的简单实现_第2张图片
第二个控制器
1.创建导航控制器为window的根控制器,设置ViewController为导航控制器的根控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:[ViewController new]];

    self.window.rootViewController = nav;
    
    [self.window makeKeyAndVisible];
    
    return YES;
}
2.创建2个子控制器Controller1,Controller2
3.在ViewController中懒加载Controller1/Controller2,并设置导航栏,添加子控制器(控制器是父子关系,那么控制器的view也必须是父子关系)
@interface ViewController ()

@property (nonatomic, strong) Controller1 *one;
@property (nonatomic, strong) Controller2 *two;
@property (weak, nonatomic) UIViewController *showingVC;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupNav];
    
    [self addChildViewController:self.one];
    [self addChildViewController:self.two];
    
    self.view.backgroundColor  = [UIColor redColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self sengenmentDidClick:0];
}

// 切换之前要移除之前的view,否则同时有2个view
- (void)sengenmentDidClick:(UISegmentedControl *)segment {

    [self.showingVC.view removeFromSuperview];
    
    self.showingVC = self.childViewControllers[segment.selectedSegmentIndex];
    
    [self.view addSubview:self.showingVC.view];
}

- (void)setupNav {
    
    UISegmentedControl *sengement = [[UISegmentedControl alloc] initWithItems:@[@"ONE",@"TWO"]];
    sengement.tintColor = [UIColor whiteColor];
    sengement.selectedSegmentIndex = 0;
    sengement.width = 160;
    sengement.height = 29;
    sengement.center = CGPointMake(SCREEN_WIDTH / 2, 75);
    [sengement setTitleTextAttributes:
     @{NSFontAttributeName : [UIFont systemFontOfSize:12],
       NSForegroundColorAttributeName:ColorWithRGB(0xff, 0xff, 0xff),
       NSBackgroundColorAttributeName:ColorWithRGB(0x12, 0x12, 0x12)}
                             forState:UIControlStateNormal];
    
    [sengement setTitleTextAttributes:
     @{NSFontAttributeName : [UIFont systemFontOfSize:12],
       NSForegroundColorAttributeName:ColorWithRGB(0x33, 0x33, 0x33),
       NSBackgroundColorAttributeName:[UIColor whiteColor]}
                             forState:UIControlStateSelected];
    [sengement addTarget:self action:@selector(sengenmentDidClick:) forControlEvents:UIControlEventValueChanged];
    
    self.navigationItem.titleView = sengement;
}

- (Controller1 *)one {
    
    if (!_one) {
        self.one = [[Controller1 alloc] init];
        self.one.view.frame = CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT);
    }
    return _one;
}

- (Controller2 *)two {
    if (!_two) {
        self.two = [[Controller2 alloc] init];
        self.two.view.frame = CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT);
    }
    return _two;
}
@end

你可能感兴趣的:(父子控制器的简单实现)