[Swift]ViewController中xib控件为空
来源:互联网 发布:linux ntpserver配置 编辑:程序博客网 时间:2024/05/16 05:57
在ViewController的xib中定义了一些控件,然后在使用的时候会因为这个控件为空而崩溃,报错为:
Swift Error fatal error: unexpectedly found nil while unwrapping an Optional value
What?
解决办法如下:
1.首先要保证正确初始化了ViewController,使用初始化方法init(nibName nibNameOrNil:String?, bundle nibBundleOrNil:NSBundle?) 初始化视图控制器(OC中对应 -(instancetype)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil),
OC中直接使用init也是可以的,这时系统会自动调用initWithNibName 方法。
/*
The designated initializer. If you subclass UIViewController, you must call the super implementation of this
method, even if you aren't using a NIB. (As a convenience, the default init method will do this for you,
and specify nil for both of this methods arguments.) In the specified NIB, the File's Owner proxy should
have its class set to your view controller subclass, with the view outlet connected to the main view. If you
invoke this method with a nil nib name, then this class' -loadView method will attempt to load a NIB whose
name is the same as your view controller's class. If no such NIB in fact exists then you must either call
-setView: before -view is invoked, or override the -loadView method to set up your views programatically.
*/
The designated initializer. If you subclass UIViewController, you must call the super implementation of this
method, even if you aren't using a NIB. (As a convenience, the default init method will do this for you,
and specify nil for both of this methods arguments.) In the specified NIB, the File's Owner proxy should
have its class set to your view controller subclass, with the view outlet connected to the main view. If you
invoke this method with a nil nib name, then this class' -loadView method will attempt to load a NIB whose
name is the same as your view controller's class. If no such NIB in fact exists then you must either call
-setView: before -view is invoked, or override the -loadView method to set up your views programatically.
*/
- (instancetype)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil
2.在viewDidLoad方法加载之前,这些xib的控件是可能为空的,所以在变量的属性观察器中使用xib控件时要注意。如:
varnaviTitle :String?{
didSet{
naviView.title = naviTitle
}
didSet{
naviView.title = naviTitle
}
}
应当在viewDidLoad之后使用这些控件。
1 0
- [Swift]ViewController中xib控件为空
- swift 加载xib的ViewController
- swift iOS8 XIB 问题 ViewController.init() xib
- Swift中使用Xib获取UIView控件,结果UIView控件的subViews为nil
- iOS中从加载的xib中得到的控件为空的问题
- iOS中从加载的xib中得到的控件为空的问题
- swift 中将xib文件和ViewController关联
- Swift 中 ViewController 默认构造方法不关联同名的xib文件
- 02 - 加载XIB(ViewController)后 View的子控件无响应中
- swift中使用xib
- iOS开发 从一个空项目创建ViewController和xib
- iOS开发 从一个空项目创建ViewController和xib
- swift 纯代码、xib自定义控件
- 11.6 如果iOS中xib 和viewcontroller 不对应
- iOS 直接加载xib文件到ViewController中
- swift:ViewController中自定义tableView简单入门
- 通过xib创建viewcontroller
- xib创建ViewController
- Python join函数 基础知识篇
- Hive部分操作总结
- 使用phpQuery轻松采集网页内容
- JetBrains使用技能分享征集大赛优秀作品——用webstorm开发NodeJS项目
- Hadoop源码分析 RPC Server端
- [Swift]ViewController中xib控件为空
- c++11之 Lambda表达式
- NYOJ 5 Binary String Matching (KMP)
- 微信语音下载到服务器 上传到七牛转换为mp3
- 采用PHP函数uniqid生成一个唯一的ID
- Swift必备开发库(高级篇)
- 最大连续子向量(分治策略和扫描算法)
- ButterKnife笔记
- 史上比较用心的纯代码实现AutoLayout