UIViewController -viewDidLoad 未被调用 [英] UIViewController -viewDidLoad not being called

查看:31
本文介绍了UIViewController -viewDidLoad 未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为 Cocoa 的新手,我在使用 Interface BuilderUIViewController 和朋友时遇到了一些问题.

Being new to Cocoa, I'm having a few issues with Interface Builder, UIViewController and friends.

我有一个 UIViewController 子类,在 xib 中定义了一个 UIView,并且控制器的视图出口连接到视图.xib 的文件所有者"被设置为 myViewcontroller 子类.

I have a UIViewController subclass with a UIView defined in a xib, and with the controller's view outlet connected to the view. The xib's "file's owner" is set as myViewcontroller subclass.

在这种情况下,以下用于加载控制器/视图(从主视图控制器)的代码没有按预期工作:

In this one instance, the following code to load the controller/view (from the main view controller) doesn't work as expected:

if ( self.myViewController == nil )
{
    self.myViewController = [[MyViewController alloc]
        initWithNibName:@"MyViewController" bundle:nil];
}

[self.navigationController 
    pushViewController:self.myViewController animated:YES];

在 MyViewController 的方法中,我放置了断点和日志消息以查看发生了什么:

In MyViewController's methods, I have placed breakpoints and log messages to see what is going on:

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        NSLog(@"initWithNibName
");
    }

    return self;
}

-(void)viewDidLoad {

    [super viewDidLoad];
    NSLog(@"viewDidLoad
");
}

预期结果

同时调用-initWithNibName-viewDidLoad方法,显示myViewController的视图.

Both -initWithNibName and -viewDidLoad methods are called, and myViewController's view is displayed.

观察结果

仅调用-initWithNibName,不显示视图.

我错过了什么吗?任何人都可以推荐任何检查吗?(特别是在非常不透明的 Interface Builder 工具中).

Have I missed something? Can anyone recommend anything to check? (Particularly in the wondrously opaque Interface Builder tool).

推荐答案

好的,我有一个部分的答案 - 也许大师可以解释更多.问题是:

Ok, I have a partial answer - maybe the gurus can explain some more. The problem is:

[self.navigationController pushViewController:myViewController animated:YES];

仔细观察,在这种情况下 self.navigationController 为零 - 因此推送消息无处可去.

Looking more closely, in this case self.navigationController is nil - so the push message is going no-where.

相反,如果我发送:

[self.view addSubview:self.myViewController.view];

然后出现视图并调用-viewDidLoad.

我不完全确定为什么在这个实例中没有设置 self.navigationController - 我唯一能想到的是 self 的子类>UIViewController 而不是 UITableViewController(pushViewController 代码的来源).

I'm not entirely sure why self.navigationController is not set in this instance - the only thing I can think of is that self is a subclass of UIViewController rather than UITableViewController (where the pushViewController code came from).

此外,尽管这些答案 否则说.另见我的问题这里.

Also, silently allowing messages to go to nil seems like a bad idea, although these answers say otherwise. See also my question here.

最终

下面评论中的答案,我已经意识到我真正追求的显示功能(鉴于 myViewController 是模态的)是:

Answers in comments below, I've realised the display function that I was actually after (given myViewController is modal) is:

[self presentModalViewController:myViewController animated:YES];

感谢大家的帮助.

这篇关于UIViewController -viewDidLoad 未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆