Swift Error Fatal error: unexpectedly found nil while unwrapPing an Optional valueWhat?
解决办法如下:
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. */ - (instancetype)initWithNibname:(nullable Nsstring *)nibnameOrNil bundle:(nullable NSBundle *)nibBundleOrNil |
2.在vIEwDIDLoad方法加载之前,这些xib的控件是可能为空的,所以在变量的属性观察器中使用xib控件时要注意。如:
varnaviTitle :String?{ dIDSet{ naviVIEw.Title = naviTitle } } |
以上是内存溢出为你收集整理的[Swift]ViewController中xib控件为空全部内容,希望文章能够帮你解决[Swift]ViewController中xib控件为空所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)