故事板加载时首先调用什么方法?
使用 Xcode 4.2,在我的应用程序中,视图加载由 segue 事件触发。 在视图控制器中首先调用哪个方法?
-(void) viewWillAppear:(BOOL)animated
有效,但它是第一个吗?
初始化发生在 Storyboard
中,看起来,在创建对象时,永远不会手动调用 init
方法。
让我澄清一下,当手动创建类的实例时,我们通常首先[[alloc]init]
它。在本例中,[init]
是第一个要执行的方法,也是进行各种初始化的好地方。
当类实例化通过 segue 事件发生时,init
方法的等效项是什么?在这种情况下,什么方法应该包含所有初始化逻辑?
Using Xcode 4.2, in my application, a view loading is triggered by a segue event. What method will be called first inside a view controller?
-(void) viewWillAppear:(BOOL)animated
works, but is it the first?
Initialization happens from the Storyboard
it seems, init
method is never manually called, upon object creation.
Let me clarify, when creating an instance of a class manually, we usually [[alloc]init]
it first. [init]
in this case, is the first method to be executed and a good place for various initializations.
What is the equivalent of init
method when class instantiation happens via a segue event? In such a case, what method should contain all initialization logic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为最好的选择是
-(void)awakeFromNib
。这只发生一次,而viewWillAppear
和viewDidLoad
等在初始化后可能会被多次调用。更新:正如下面 Jean-Denis Muys 所指出的,对于初始化程序来说,
-(id)initWithCoder:(NSCoder *)decoder
是更好的选择,因为-(void) 只被调用一次awakeFromNib
有可能被多次调用。I think the best option is
-(void)awakeFromNib
. This only occurs the once, whereasviewWillAppear
andviewDidLoad
etc could be called more than once after your initialisation.UPDATE: As pointed out by Jean-Denis Muys below,
-(id)initWithCoder:(NSCoder *)decoder
is a better option for an initialiser that only gets called once as-(void)awakeFromNib
has the potential to be called more than once.根据苹果的 查看控制器编程指南,
According to Apple's View Controller Programming Guide,
我建议不要使用 awakeFromNib。我只使用这两个函数
因为:假设您对 UIButton 进行了子类化。您必须准备好两种场景:
场景 1。如果您以编程方式添加按钮 --> initWithFrame->setupUI 将被调用
场景2.如果使用NIb添加按钮--> initWithCoder->setupUI 将被调用。
I would advise not to use awakeFromNib. I use simply both of these functions
because: let's say you subclassed UIButton. You must be ready for two scenario:
scenario 1. If you add the button programmatically --> initWithFrame->setupUI will be called
scenario 2. If you add the button using NIb --> initWithCoder->setupUI will be called.