问题描述
我创建了UITabBarController
,每个选项卡包含UINavigationController
,并在此UINavigationController
中设置了rootviewcontroller,所有这些操作均在界面生成器中完成.在viewDidLoad中,我尝试从视图中获取帧大小,但是当我引用视图时,它返回null.以前有没有人经历过.
I created UITabBarController
with each tab contain UINavigationController
and set rootviewcontroller in this UINavigationController
, all of this is done in interface builder. In viewDidLoad I try to get frame size from view, but when I reference view it return null. Have anyone experienced this before.
所有IBoutlet均已正确连接.
All IBoutlet are properly connected.
推荐答案
viewDidLoad
如果在您的.xib
中连接了视图,则返回nil视图可能意味着:
viewDidLoad
returning a nil view if the view is connected in your .xib
could mean:
对于程序初始化(自定义控制器):
For programmatic initialisation (custom controllers):
- 您忘记总共在视图控制器类上调用
initWithNibName:bundle:
(您可能已经调用了init
或其他名称). - 您已经在视图控制器类中覆盖了
loadView
方法,但是它没有设置view
.
- You forgot to call
initWithNibName:bundle:
on the view controller class altogether (you may have calledinit
or something instead). - You've overridden the
loadView
method in your view controller class, but it doesn't set theview
.
对于所有控制器:
- 未正确建立插座连接.
- 在显示视图或视图控制器之前,您不小心将其释放了.
- 初始化视图时,未正确指定
nibName
参数(找不到笔尖或找到了一个没有视图的笔尖.尽管这也会引发异常). - 没有足够的内存来分配
view
(尽管该应用可能到那时将被终止).
- An outlet connection hasn't been made correctly.
- You have accidentally released the view or view controller before showing it.
- The
nibName
parameter was not properly specified when initialising the view (the nib could not be found or one without a view was found.. though this should also throw an exception). - There wasn't enough memory to allocate the
view
(the app would likely have been terminated by that point though).
我建议您改为尝试在viewWillAppear:
中进行帧大小计算,然后查看view
在那时是否仍为nil
.
I'd recommend you try doing frame size calculations in viewWillAppear:
instead and see if the view
is still nil
at that point.
这篇关于在viewDidLoad中,视图为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!