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

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

问题描述

作为Cocoa新手,我遇到了 Interface Builder UIViewController 和朋友的几个问题。 / p>

我有一个 UIViewController 子类与 UIView 一个xib,并且控制器的视图出口连接到视图。 xib的文件所有者设置为 myViewcontroller 子类。



在这一个实例中,加载控制器/视图主视图控制器)不能按预期工作:

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

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



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

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

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

return self;
}

- (void)viewDidLoad {

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

预期结果 b

调用 -initWithNibName -viewDidLoad 方法,并显示myViewController的视图。


$ b

观察结果



只有 -initWithNibName 被调用,视图不显示。



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

解决方案

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

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

更仔细地看,在这种情况下 self.navigationController

如果我发送:



<$> p $ p> [self.view addSubview:self.myViewController.view];

然后视图出现, -viewDidLoad 调用。



我不完全确定为什么 self.navigationController 没有设置在这个实例 - 我可以想到的是 self UIViewController 的子类,而不是 UITableViewController pushViewController 代码来自)。



此外,默认允许消息去nil似乎是一个糟糕的主意,尽管这些答案不同。另请参阅我的问题此处



最后编辑:



我实际上后(给myViewController是模态)是:

  [self presentModalViewController:myViewController animated:YES]; 

感谢大家的帮助。


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

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];

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\n");
    }

    return self;
}

-(void)viewDidLoad {

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

Expected result

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

Observed result

Only -initWithNibName is called, the view is not displayed.

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];

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

Instead, if I send:

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

Then the view appears and -viewDidLoad is called.

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.

Final edit:

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];

Thanks everyone for their helpful responses.

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

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