您的位置:首页 > 其它

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

2015-09-26 19:51 435 查看
了解
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: