利用addChildViewController管理子视图控制器<2>

来源:互联网 发布:个性签名 知乎 编辑:程序博客网 时间:2024/06/05 19:59

了解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
0 0