标签:
1 #import "RootViewController.h" 2 #import "LoginViewController.h" 3 #import "RegistViewController.h" 4 #import "PasswardViewController.h" 5 #import "RootView.h" 6 7 @interface RootViewController () 8 @property (nonatomic, strong) RootView *rootView; 9 @property (nonatomic, strong) LoginViewController *loginVC; 10 @property (nonatomic, strong) RegistViewController *registVC; 11 @property (nonatomic, strong) PasswardViewController *passwardVC; 12 13 @end 14 15 @implementation RootViewController 16 17 - (void)loadView { 18 self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 19 self.view = self.rootView; 20 21 } 22 23 24 - (void)viewDidLoad { 25 [super viewDidLoad]; 26 27 // 初始化视图控制器 28 self.loginVC = [[LoginViewController alloc] init]; 29 self.registVC = [[RegistViewController alloc] init]; 30 self.passwardVC = [[PasswardViewController alloc] init]; 31 32 33 // segment添加一个事件 34 [self.rootView.segment addTarget:self action:@selector(click:) forControlEvents:UIControlEventValueChanged]; 35 36 } 37 38 39 // 实现点击事件 40 - (void)click:(UISegmentedControl *)sender { 41 42 switch (sender.selectedSegmentIndex) { 43 case 0: 44 // 移除父视图中最上面的子视图 45 [[self.rootView.subviews objectAtIndex:0] removeFromSuperview]; 46 47 // 添加新视图 48 [self.rootView addSubview:self.loginVC.view]; 49 50 // 把分段选择器显示在最外层 51 [self.rootView bringSubviewToFront:self.rootView.segment]; 52 break; 53 54 case 1: 55 // 移除父视图中最上面的子视图 56 [[self.rootView.subviews objectAtIndex:0] removeFromSuperview]; 57 58 // 添加新视图 59 [self.rootView addSubview:self.registVC.view]; 60 61 // 把分段选择器显示在最外层 62 [self.rootView bringSubviewToFront:self.rootView.segment]; 63 break; 64 65 case 2: 66 // 移除父视图中最上面的子视图 67 [[self.rootView.subviews objectAtIndex:0] removeFromSuperview]; 68 69 // 添加新视图 70 [self.rootView addSubview:self.passwardVC.view]; 71 72 // 把分段选择器显示在最外层 73 [self.rootView bringSubviewToFront:self.rootView.segment]; 74 break; 75 76 default: 77 break; 78 } 79 } 80 @end
标签:
原文地址:http://www.cnblogs.com/zhizunbao/p/5424380.html