xcode 故事板中的 initWithNibName 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11973054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
initWithNibName method in storyboard
提问by Bob Jones
I am following Facebook's tutorial to post to a user's wall: http://developers.facebook.com/docs/howtos/publish-to-feed-ios-sdk/and although it is made with a .xib project in mind, I have been able to do everything just fine with my storyboard project so far. However, I have come to the point where I need to put some code into the initWithNibName:bundle: method, but I cannot do this because I am using storyboard. And, since it's hard to tell when exactly the method would be called, I can't just write a method named initWithNibName:bundle: and call it from some other method. If anyone has any idea of how to solve my issue, please say so. Thanks.
我正在按照 Facebook 的教程发布到用户的墙上:http: //developers.facebook.com/docs/howtos/publish-to-feed-ios-sdk/虽然它是用 .xib 项目制作的,但我到目前为止,我的故事板项目已经能够很好地完成所有工作。但是,我已经到了需要将一些代码放入 initWithNibName:bundle: 方法的地步,但我不能这样做,因为我使用的是故事板。而且,由于很难确定该方法的确切调用时间,所以我不能只编写一个名为 initWithNibName:bundle: 的方法并从其他方法调用它。如果有人对如何解决我的问题有任何想法,请说出来。谢谢。
回答by Kitsune
I just ran into this problem myself. Storyboards appear to not use initWithNibName:bundle:
, but either initWithCoder:(NSCoder *)aDecoder
or initWithStyle:(UITableViewStyle)style
.
我自己刚刚遇到了这个问题。故事板似乎不使用initWithNibName:bundle:
,而是使用initWithCoder:(NSCoder *)aDecoder
或initWithStyle:(UITableViewStyle)style
。
The default implementations of the two methods look like:
这两种方法的默认实现如下所示:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
// Custom initialization
}
return self;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
I've yet to use the initWithStyle version myself, but I'd imagine it's called for a UITableViewController. If in doubt, you could simply add both methods to the file, along with an NSLog() call that prints the method's name (or any other unique string). You can then run it in the simulator to see which is called, and delete the other.
我自己还没有使用过 initWithStyle 版本,但我认为它需要一个 UITableViewController。如果有疑问,您可以简单地将这两个方法添加到文件中,以及打印方法名称(或任何其他唯一字符串)的 NSLog() 调用。然后您可以在模拟器中运行它以查看哪个被调用,并删除另一个。
I'd strongly recommend against calling initWithNibName:bundle:
yourself from any of the other init
methods. Better to just move the code to the correct method.
我强烈建议initWithNibName:bundle:
不要通过任何其他init
方法调用自己。最好将代码移动到正确的方法。