标签:ios ib 子控制器 child controller
原创Blog,转载请注明出处 
blog.csdn.net/hello_hwc
准备工作 
Storyboard上为一个ViewController拖拽两个子控制器,并且设置两个segue的identifier分别为childvc1,childvc2 
 
效果 
#import "ViewController.h"
#import "ChildViewController1.h"
#import "ChildViewController2.h"
@interface ViewController ()
@property (weak,nonatomic)ChildViewController1 * childvc1;
@property (weak,nonatomic)ChildViewController2 * childvc2;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.childvc1.view.backgroundColor = [UIColor blueColor];
    self.childvc2.view.backgroundColor = [UIColor greenColor];
    // Do any additional setup after loading the view, typically from a nib.
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"childvc1"]) {
        self.childvc1 = segue.destinationViewController;
    }
    if ([segue.identifier isEqualToString:@"childvc2"]) {
        self.childvc2 = segue.destinationViewController;
    }
}
@end
两点要注意
#import "ViewController.h"
#import "ChildViewController1.h"
#import "ChildViewController2.h"
@interface ViewController ()
@property (weak,nonatomic)ChildViewController1 * childvc1;
@property (weak,nonatomic)ChildViewController2 * childvc2;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.childvc1.view.backgroundColor = [UIColor blueColor];
    self.childvc2.view.backgroundColor = [UIColor greenColor];
    // Do any additional setup after loading the view, typically from a nib.
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([self respondsToSelector:NSSelectorFromString(segue.identifier)]) {
        [self setValue:segue.destinationViewController forKey:segue.identifier];
    }
}
@end
注意,这里的
segue的identifier一定要和声明的对应子控制器的属性一致。
原理-利用KVC的动态特性
iOS 获取Interface Builder上的子控制器的两种方式
标签:ios ib 子控制器 child controller
原文地址:http://blog.csdn.net/hello_hwc/article/details/45788581