View Controller Initialization

本文详细阐述了在iOS开发中,ViewController初始化时的四个重要时机:initWithNibName:bundle:、awakeFromNib、viewDidLoad和viewWillAppear:。这些时机分别适用于不同类型的初始化任务,帮助开发者高效地管理ViewController的生命周期。

Four places to initialize things in View Controller subclasses

你有四个机会来做初始化工作

  • - (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle; (i.e. override it)
  • - (void)awakeFromNib
  • - (void)viewDidLoad
  • - (void)viewWillAppear:(BOOL)animated;

Designated Initializer

Usually only for things that have to be initialized for your view controller to even “make sense”. Often thought of as the place to initialize things having to do with your Model. Definitely not for initializing things in your View (some “UI-related” things ok like self.title).

用于初始化一些跟model有关的工作.不能用来初始化跟你的view相关的东西(比如self.title之类跟UI相关的东西)

awakeFromNib

Same purpose (generally) as your designated initializer. This is called on every object that comes out of a .xib file (instead of its designated initializer!). Sometimes UIViewControllers come out of a .xib file (e.g. MainWindow.xib), but sometimes not. Usually you want your VC to work when it is alloc/inited or if it comes from a .xib. So create a method (e.g. setup) and call it from initWithNibName:bundle: and awakeFromNib.

跟之前的用途差不多.不过每次从.xib文件中反序列化的时候都会被调用(而不是在initializer里面).有时候UIViewController也会从.xib文件中取出.

<!-- p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px SimSun} span.s1 {font: 9.0px Arial} span.s2 {font: 12.0px SimSun} -->应用程序在装载 nib 文件时,会向从档案中装载的对象发送一个 awakeFromNib 消息,但只是发送给可以响应该消息的对象,且在档案中的所有对象都装载和初 始化完成后发送。当对象接收到 awakeFromNib 消息时,Cocoa 会保证对象中所有的插座实例变 量都准备好了。典型情况下,拥有 nib 文件的对象(File’s Owner)会实现 awakeFromNib 方法, 以执行插座变量和目标-动作连接准备好之后才能执行的初始化工作。

viewDidLoad

This is the best place to put non-geometry-related initialization code which pertains to your View. You might even add some more views to your hierarchy in this method (stuff you couldn’t do in IB). Wouldn’t be out of the question to put some Model initialization code here, but theoreticallythis method could be called multiple times, so don’t re-initialize something already initialized. It’s not totally unheard of to have Views which are loaded, but then never appear on screen. So consider viewWillAppear: for some things, but maybe check to avoid multiple initialization.

这是用来初始化一些跟绘图无关代码的最佳地方.你还可以为你的图层增加更多的view(那些不能在IB里面添加的东西).毋庸置疑你也可以放一些model有关的代码,不过理论上这个方法可能会被调用多次,所以不要重复初始化.还有并不是没有这种情况出现,比如说view装载以后从来没有显示过这种情况.所以这时应该考虑viewWillAppear:

viewWillAppear:

If you have initialization that depends on your View’s bounds being set, you must do it here (and not in viewDidLoad).

如果有一些必须依赖view的bounds的代码,就放这好了

VMware_View问题汇总指南 1、启连接服务器提示无法验证服务器 ............................................................................. 3 2、部署linked clone虚拟桌面失败,提示如下错误提示:“View Composer agent initialization state error (18): Failed to join the domain” ................................................ 4 3、PCoIP连接时不能跨两个显示器显示 ........................................................................... 5 4、确认桌面资源池Desktop Pool存在 .............................................................................. 7 5、访问View Security服务器失败:Give final block not properly padded ...................... 8 6、为View Composer创建一个QuickPrep账户的步骤 ................................................... 8 7、登录到虚拟桌面时,提示“Login in as current user”选项失败 .................................... 9 8、View4.5安装View Agent报错:28051 Shared Access错误 ..................................... 10 9、删除VIEW的孤立的persistent diskVMware .............................................................. 11 10、ThinApp打包和运维最佳实践 ................................................................................... 11 11、手动从VMware View Manager删除linked clones或陈旧的virtual desktop ........ 12 12、尝试在VMware View 5.0的Session Manager对话框里尝试删除Session提示:clock skewed .................................................................................................................. 13 13、VMware View 5.0的限制和Maximum说明 ............................................................. 14 14、远程登录桌面后,黑屏后登出的问题 ..................................................................... 14 15、PCoIP连接View 5.0虚拟桌面超时断开导致无法再次登录的问题 ....................... 15 16、关于View 5.x中Adobe Flash Throttling的参数详解 ............................................... 16 17、解决:Unable to retrieve information from Active Directory for domain null .......... 16 18、2008安装View Connection Server失败:There was an error creating a MS Direct17 19、安装View Connection Server replica失败:Error 28018 ......................................... 18 20、激活了自动登陆 View Client 导致虚拟桌面连接失败的解决方案 ....................... 18 21、删除掉 VMware View Composer(Link
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值