相关文章:http://jianyu996.blog.163.com/blog/static/1121145552012102293653906/
初学iOS开发,目前尝试用xib来构建UI
构建时有2种常用的方式:initWithNibName和loadNibNamed
我最初接触到的,是自定义一个复杂的UITableViewCell
1 2 3 |
MyCell *cell = (MyCell*)[[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil][0]; cell.data = data; |
然后在setData里做一些替换UI文字和图片的工作
于是乎,下一个ViewController我也这么做
1 2 3 |
MyViewController *viewController = (MyViewController*)[[NSBundle mainBundle] loadNibNamed:@"MyViewController" owner:self options:nil][0]; [self.navigationController pushViewController: viewController animated:YES]; |
报错 发现[[NSBundle mainBundle] loadNibNamed:@”MyViewController” owner:self options:nil][0]是一个UIView对象
没关系,UIViewController不是还提供了initWithNibName:bundle:方法么,于是乎
1 2 3 |
MyViewController *viewController = [MyViewController initWithNibName:@"MyViewController" bundle:nil];//然后在构造函数里对界面元素进行修改 [self.navigationController pushViewController: viewController animated:YES]; |
编译通过,但是奇怪的是,initWithNibName:bundle:里的自定义代码没有生效,发现xib里映射的对象全是nil
经过搜索查证,发现应该把自定义代码放在viewDidLoad里面
其实Xcode在生成文件模版的时候也提示开发者了
init的时候Custom initialization
viewDidLoad才是Do any additional setup after loading the view