利用addChildViewController管理子视图控制器<2>
2015-09-26 19:51
435 查看
了解
但是奇怪的是,我按照网上的博客教程Coding,发现老是出问题,在这里贴下代码:
如图,只要在代码中使用到所创建控制器的
在这里提到,以上代码中父控制器控制的字控制器都是在父控制器中创建的,不知道是不是这个原因呢?
在网上找了好长时间,也没能找到问题的答案。我换种思路,便完成了效果:
核心:将子控制器作为父控制器的属性传入,感觉有点像
addChildViewController之后,我们就要开始Coding学习该知识点了。
但是奇怪的是,我按照网上的博客教程Coding,发现老是出问题,在这里贴下代码:
#import "ViewController.h" #import "SSone.h" #import "SStwo.h" @interface ViewController () @property(nonatomic,strong)SStwo* two; @property(nonatomic,strong)SSone* one; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSArray* arr=[NSArray arrayWithObjects:@"联系人",@"QQ通话", nil]; UISegmentedControl* seg=[[UISegmentedControl alloc] initWithItems:arr]; seg.selectedSegmentIndex=0; [seg addTarget:self action:@selector(selectedValueChange:) forControlEvents:UIControlEventValueChanged]; self.navigationItem.titleView=seg; self.one=[[SSone alloc] init]; [self addChildViewController:self.one]; [self.view addSubview:self.one.view]; self.two=[[SStwo alloc] init]; [self addChildViewController:self.two]; } -(void)selectedValueChange:(id)sender{ UISegmentedControl* seg=(UISegmentedControl*)sender; if (seg.selectedSegmentIndex==0) { }else if (seg.selectedSegmentIndex==1){ } } @end
如图,只要在代码中使用到所创建控制器的
view属性,就会使应用崩溃。
在这里提到,以上代码中父控制器控制的字控制器都是在父控制器中创建的,不知道是不是这个原因呢?
在网上找了好长时间,也没能找到问题的答案。我换种思路,便完成了效果:
核心:将子控制器作为父控制器的属性传入,感觉有点像
UITabBarController管理子控制器一样。不说废话,贴代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; SSone* one=[[SSone alloc] init]; SStwo* two=[[SStwo alloc] init]; ViewController* contrl=[[ViewController alloc] init]; contrl.controllers=@[one,two]; self.window.rootViewController=[[UINavigationController alloc] initWithRootViewController:contrl]; [self.window makeKeyAndVisible]; return YES; }
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @property(nonatomic,strong)NSArray* controllers; @end
#import "ViewController.h" @interface ViewController () @property(nonatomic,weak)UISegmentedControl* seg; @property(nonatomic,assign)NSInteger current; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UISegmentedControl* seg=[[UISegmentedControl alloc] initWithItems:@[@"one",@"two"]]; self.seg=seg; self.navigationItem.titleView=self.seg; [self.seg addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged]; NSInteger count=self.controllers.count; for (int i=0; i<count; i++) { UIViewController* contrl=self.controllers[i]; [self addChildViewController:contrl]; contrl.view.frame=self.view.frame; } [self.view addSubview:((UIViewController*)(self.controllers[0])).view]; self.current=0; self.seg.selectedSegmentIndex=0; } -(void)valueChanged:(id)sender{ NSLog(@"valueChanged"); UISegmentedControl* seg=(UISegmentedControl*)sender; NSInteger select=seg.selectedSegmentIndex; [self transitionFromViewController:((UIViewController*)(self.controllers[self.current])) toViewController:((UIViewController*)(self.controllers[select])) duration:1 options:UIViewAnimationOptionTransitionNone animations:^{ } completion:^(BOOL finished) { if (finished) { self.current=select; } }]; } @end
相关文章推荐
- mysql命令行
- 地址中含有汉字等其他字符时 的处理方法
- 基于MSP430G2系列实现的步进电机控制
- 自主学习,以开放的实践
- [leetcode 48] rotate image
- 索引
- linux grep命令详解
- Apache反向代理部署Web应用
- 输入一个字符统计其中有多少个单词。
- 一步一步学jQuery(一)
- 学习python处理python编码问题
- 应该是什么在预新手发“外链”(4)最终的外链的方法
- pat1040有多少个pat。如何解决超时问题
- hdu 5475 An easy problem
- 12th_usb(4th)
- 放眼区块链掘金而不是比特币掘金
- python编码
- java关于转义字符“\”
- 避免死锁
- 进程调度与作业调度