努力学习着......
从xib/nib加载UIViewController/或其子类【之所以】 xib、nib文件目的在于为控件快速定位 这里仅针对UIViewController和其子类 【How】 这里整理并描述两种方式的使用: 1、UIViewController initWithNibName 主要代码如下: StarterController *starter = [[StarterController alloc] initWithNibName:@"StarterController" bundle:nil]; NSLog(@"starter = %@" ,starter); if (starter) { NSLog(@"%@ parent controller is %@" ,starter ,starter.parentViewController); NSLog(@"%@ nav controller is %@" ,starter ,starter.navigationController); } 注意参数bundle,暂时不知道怎么获取,后续解答。 StarterController.xib,如图,此处File Owner ,Class = StarterController,此类继承UIViewController 2、NSBundle loadNibNamed 主要代码如下: NSArray *niblets = [[NSBundle mainBundle] loadNibNamed:@"sample" owner:self options:NULL]; for (id theObject in niblets) { if ([theObject isKindOfClass:[UIViewController class]]) [self.navigationController pushViewController:theObject animated:YES]; } sample.xib,如图,特别注意File Owner,这个类是NSObject 【其他】 在实践中,出现一个问题:“loaded the...nib but the view outlet was not set” 解决参见: http://hi.baidu.com/sushii/blog/item/17df66084dd7abdd3ac763bb.html |
|