IOS obtains the sub-Controller on Interface Builder in two ways: iosbuilder
Original Blog, reprinted, please indicate the source
Blog.csdn.net/hello_hwc
Preparations
On the Storyboard, drag and drop two sub-controllers for a ViewController, and set the identifiers of the two sews to childvc1 and childvc2 respectively.
Effect
Method 1: determine the result based on the identifier
#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
Note:
Method 2: Use the features of KVC to create some common code
#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
Note:
The identifier must be consistent with the declared attributes of the corresponding sub-controller.
Principle-using the dynamic features of KVC