iPhone 中未调用 ViewDidLoad 方法?
我已使用自定义 init 方法将 NSMutableDictionary 传递到 FirstClass 中。
第二类,
firstClass = [[FirstClass alloc] initWithFeedData:feedDictionary];
[self.navigationController pushViewController:firstClasss animated:YES];
第一类,
- (FirstClass *) initWithFeedData:(NSMutableDictionary *) dataDictionary {
{
NSLog(@"%@", dataDictionary);
[[NSBundle mainBundle] loadNibNamed:@"FirstClass" owner:self options:nil];
//[self viewDidLoad]; Without call, ViewDidLoad method doesn't call.
}
- (void) viewDidLoad {
[super viewDidLoad];
NSLOG(@"Inside View DidLoad"); // This method never calls.
}
在我的 viewDidLoad 方法中没有调用,如果我调用“[self viewDidLoad]”,那么只有 viewDidLoad 方法才能正常工作。我不知道为什么 viewDidLoad 方法不直接调用而不调用另一个方法?请帮帮我。
谢谢。
I have passed the NSMutableDictionary into the FirstClass using custom init method.
Second Class,
firstClass = [[FirstClass alloc] initWithFeedData:feedDictionary];
[self.navigationController pushViewController:firstClasss animated:YES];
FirstClass,
- (FirstClass *) initWithFeedData:(NSMutableDictionary *) dataDictionary {
{
NSLog(@"%@", dataDictionary);
[[NSBundle mainBundle] loadNibNamed:@"FirstClass" owner:self options:nil];
//[self viewDidLoad]; Without call, ViewDidLoad method doesn't call.
}
- (void) viewDidLoad {
[super viewDidLoad];
NSLOG(@"Inside View DidLoad"); // This method never calls.
}
In my viewDidLoad method doesn't called, if i call "[self viewDidLoad]" then only, viewDidLoad method works properly. I donno why viewDidLoad method doesn't call directly without calls in another method? Please Help me out.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您正在子类化
UIViewController
,我认为您是因为您期待viewDidLoad
,那么您应该重写其指定的初始化程序initWithNibName:bundle:< /code> 如果您使用 XIB,在这种情况下,
viewDidLoad
将在 XIB 加载后调用。如果您不使用 XIB,则应实现loadView
方法来创建视图,并且viewDidLoad
将在loadView
完成后调用。If you're subclassing a
UIViewController
, which I assume you are since you're expectingviewDidLoad
, then you should override its designated initialiser,initWithNibName:bundle:
if you're using a XIB, in which caseviewDidLoad
will be called after the XIB loads. If you're not using a XIB, you should implement theloadView
method to create your view, andviewDidLoad
will be called afterloadView
finishes.@Stephen是对的,至少你需要重写你的init语句以返回
self
。但是,在视图控制器上声明属性并以这种方式传递对象要更容易、更健壮。您的
viewDidLoad
方法现在将被调用,您的feedDictionary
将坐在那里等待您。@Stephen is right, at the very least you need to rewrite your init statement to return
self
. However, it's much easier and more robust to declare a property on your view controller and pass objects in that manner.Your
viewDidLoad
method will now be called and yourfeedDictionary
will be sitting there, waiting for you.您的
init
方法不正确。它应该看起来像这样:假设
FirstClass
派生自UIViewController
。只要您的 NIB 文件与您的类具有相同的名称,它就应该自动加载 - 不需要
loadBundle:
调用。以下是文档的内容nibName
属性:Your
init
method is not correct. It should look something like this:This assumes that
FirstClass
is derived fromUIViewController
.As long as your NIB file has the same name as your class it should be loaded in automatically -- there's no need for the
loadBundle:
call. Here's what the documentation says about thenibName
property: