iOS --- UIViewController中的loadView使用场景
2015-09-11 13:03
597 查看
对之前的内容进行了补充及截图说明。
因为还要做其他的实验, 所以使用了Test1ViewController及实例test1VC,而之前是使用TestViewController及testViewController,这点应该不影响大家阅读。
imageViewCourse和lbCourse是其两个属性,通过nib的IBOutlet方式添加。如下图:
[thead]
使用如下代码进行UIViewController之间的跳转:
我们使用nib来加载一个TestViewController并对其属性赋值, 然后跳转。问题在于执行完initWithNibName之后,testViewController.imageViewCourse和testViewController.lbCourse都为nil, 则表现出来的是跳转到TestViewController之后, 其中的imageViewCourse和lbCourse中没有内容。如下图:
[thead]
即:
结果如下图:
[thead]
代码如上, 不做过多解释。三种方式其实殊途同归。
因为还要做其他的实验, 所以使用了Test1ViewController及实例test1VC,而之前是使用TestViewController及testViewController,这点应该不影响大家阅读。
问题
先看代码, 新建一个UIViewController的子类TestViewController(包含nib文件):imageViewCourse和lbCourse是其两个属性,通过nib的IBOutlet方式添加。如下图:
新建Test1ViewController | 添加IBOutlet |
---|---|
TestViewController *testViewController = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil]; testViewController.imageViewCourse.image = image; testViewController.lbCourse.text = course; [self presentViewController:testViewController animated:YES completion:nil];
我们使用nib来加载一个TestViewController并对其属性赋值, 然后跳转。问题在于执行完initWithNibName之后,testViewController.imageViewCourse和testViewController.lbCourse都为nil, 则表现出来的是跳转到TestViewController之后, 其中的imageViewCourse和lbCourse中没有内容。如下图:
IBOutlet未更新内容 | 断点调试 |
---|---|
解决方法
使用loadView方法触发nib中UIView的加载。@property(null_resettable, nonatomic,strong) UIView *view; // The getter first invokes [self loadView] if the view hasn't been set yet. Subclasses must call super if they override the setter or getter. - (void)loadView; // This is where subclasses should create their custom view hierarchy if they aren't using a nib. Should never be called directly. - (void)loadViewIfNeeded NS_***AILABLE_IOS(9_0); // Loads the view controller's view if it has not already been set.
即:
TestViewController *testViewController = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil]; [testViewController view]; //[testViewController loadView]; //[testViewController loadViewIfNeeded]; testViewController.imageViewCourse.image = image; testViewController.lbCourse.text = course; [self.navigationController presentViewController:testViewController animated:YES completion:nil];
结果如下图:
IBOutlet更新内容 | 断点调试 |
---|---|
相关文章推荐
- iOS KVC setValuesForKeysWithDictionary的使用
- UI线程和工作线程
- iOS KVC setValuesForKeysWithDictionary的使用
- 解决UICollectionView内容填充不满无法下拉的问题
- UiTableview的常用小知识汇集
- android:allowBackup=true 数据备份(adb backup)+查看(abe unpack backup.ab backup.tar)+恢复(adb re)
- [LeetCode 232] Implement Queue using Stacks
- easy ui 下拉级联效果 ,下拉框绑定数据select控件
- 代码手写UI,xib和StoryBoard间的博弈,以及Interface Builder的一些小技巧
- easy ui 下拉框绑定数据select控件
- String、StringBuffer、StringBuilder
- easyui常用控件及样式收藏
- UITextView垂直方向上光标和文字显示文字的问题
- Android UI系列--对话框(一)(AlertDialog,TimePickerDialog,DatePickerDialog,ProgressDialog)
- UIColor+Hex
- css3之 media query 使用(转)
- UIView 坐标转换
- burpsuite+sqlmap跨登录验证SQL注入
- Druid数据源配置
- [MySQL FAQ]系列 — 线上环境到底要不要开启query cache