简体   繁体   English

-viewDidLoad未在子类UIViewController中调用

[英]-viewDidLoad not called in subclassed UIViewController

Have patience with me, I'm still learning Cocoa Touch. 对我有耐心,我还在学习Cocoa Touch。 Other -viewDidLoad not being called question was unrelated to my issue, I did search. 其他-viewDidLoad没被称为问题与我的问题无关,我做了搜索。

I have FooViewController , a UIViewController subclass. 我有FooViewController ,一个UIViewController子类。 FooViewController.xib has its File's Owner set to FooViewController. FooViewController.xib将其File's Owner设置为FooViewController。 Additionally, I have Main , whose App Delegate is MyApplication and whose File's Owner is UIApplication . 另外,我有Main ,其App Delegate是MyApplication ,其文件所有者是UIApplication My primary nib is set to Main. 我的主要笔尖设置为Main。

I am able to make the view appear on the screen, using this flow (code snipped for brevity): 我可以使用此流程(为简洁起见,代码剪切)使视图显示在屏幕上:

UIWindow *main;

-(void)applicationDidFinishLaunching:(UIApplication*)application {
    [self showFooViewController];
    [main makeKeyAndVisible];
}

-(void)showFooViewController {
    fooViewController = [[FooViewController alloc] init];
    if(![[NSBundle mainBundle] loadNibNamed:@"FooViewController" owner:fooViewController options:nil]) {
        DebugF(@"Failed to load FooViewController");
        return;
    }

    // Add the view and put it in place
    UIView *view = [fooViewController view];
    CGRect cur = [view frame];
    cur.origin = CGPointMake(0.0f, 20.0f);
    [view setFrame:cur];

    [main addSubview:[fooViewController view]];
}

However, this message is not being sent to FooViewController : 但是,此消息未发送到FooViewController

- (void) viewDidLoad {
    DebugF(@"Hello!");
}

Total silence in gdb's output. gdb输出中的总静音。

I've seen other Cocoa tutorials that show Instances and such in IB's document view, but I do not see these options in mine. 我已经在IB的文档视图中看到过显示实例的其他Cocoa教程,但是我没有看到这些选项。 Am I supposed to be instantiating fooViewController in the Nib? 我应该在Nib中实例化fooViewController吗? Did I miss a connection? 我错过了连接吗? In FooViewController.xib, File's Owner view is connected to the view in the Nib. 在FooViewController.xib中,File的Owner视图连接到Nib中的视图。

I'm sure this is a simple solution, and I have wandered down the wrong path. 我确信这是一个简单的解决方案,而且我走错了路。 Halp! HALP!

I have not found documentation to back up the following, but this is what I've deduced by experiment / experience: 我没有找到备份以下内容的文档,但这是我通过实验/经验推断的:

The viewDidLoad method is expected be called immediately after loadView is called, and it's up to whomever calls loadView to supply the extra call to viewDidLoad . 预期在调用loadView之后立即调用viewDidLoad方法,并且由任何调用loadView来提供对viewDidLoad的额外调用。

In some cases, the SDK will handle this behavior for you. 在某些情况下,SDK会为您处理此行为。 Suppose you have a subclass of UIViewController called MyViewController ; 假设你有一个名为MyViewControllerUIViewController的子类; then: 然后:

  • If you access myViewController.view before you call loadView , then the superclass accessor is smart enough to call loadView for you, and viewDidLoad immediately thereafter. 如果在调用loadView之前访问myViewController.view ,那么超类访问器足够聪明,可以为您调用loadView ,之后立即调用viewDidLoad

As a result, if your code looks like this: 因此,如果您的代码如下所示:

MyViewController *myViewController = [[MyViewController alloc] init];
[superView addSubview:myViewController.view];  // Calls viewDidLoad.

then both loadView and viewDidLoad will be called on your behalf. 然后将代表您调用loadViewviewDidLoad

However, if your code looks like this: 但是,如果您的代码如下所示:

MyViewController *myViewController = [[MyViewController alloc] init];
[myViewController loadView];
[superView addSubview:myViewController.view];  // Doesn't call viewDidLoad.

then the view getter can see you've already loaded the view, and it assumes you also called viewDidLoad as well - so it doesn't call either. 然后view getter可以看到你已经加载了视图,并且它假设你也调用了viewDidLoad - 所以它也没有调用。 Ironically, the extra function call here prevents viewDidLoad from getting called. 具有讽刺意味的是,此处的额外函数调用会阻止调用viewDidLoad

I have had this same problem twice. 我有两次同样的问题。 The first time, I forgot to connect the view of UITableViewController to the actual UITableView in interface builder. 第一次,我忘了将UITableViewController的视图连接到界面构建器中的实际UITableView。 The second time, I forgot to make awakeFromNib call itself in the superclass. 第二次,我忘了让awakeFromNib在超类中调用自己。

And I've answered my own question, so this may help future folks. 而且我已经回答了我自己的问题,所以这可能对未来的人有所帮助。 Since I see viewDidLoad has, as its three top Googles in my suggest, "viewDidLoad not called", "viewDidLoad not getting called", and "viewDidLoad not firing", I imagine this is a usual mistake... 既然我看到viewDidLoad有,作为我建议的三个顶级谷歌,“viewDidLoad没有被调用”,“viewDidLoad没有被调用”,“viewDidLoad没有被激活”,我想这是一个常见的错误......

Changing: 更改:

fooViewController = [[FooViewController alloc] init];
if(![[NSBundle mainBundle] loadNibNamed:@"FooViewController" owner:fooViewController options:nil]) {
    DebugF(@"Failed to load FooViewController");
    return;
}

To: 至:

fooViewController = [[FooViewController alloc] initWithNibName:@"FooViewController" bundle:[NSBundle mainBundle]];

Fixed the problem. 解决了这个问题。 Obviously a mistake in how I'm using Cocoa. 显然是我如何使用Cocoa的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UIViewController -viewDidLoad 未被调用 - UIViewController -viewDidLoad not being called UIViewController和UITabBarController应用-viewDidLoad仅调用一次 - UIViewController and UITabBarController app - viewDidLoad called only once 推送UIViewController时未调用viewDidLoad - viewDidLoad not being called when pushing UIViewController 从笔尖加载的UIViewController:未调用-viewDidLoad - UIViewController loaded from nib: -viewDidLoad not being called UIViewController中的ViewDidLoad方法 - 什么时候被调用? - ViewDidLoad method in UIViewController - when does it get called? UINavigationController的一种方法,作为UIViewController的“ viewDidLoad” - A method to UINavigationController as “viewDidLoad” to UIViewController 首次显示视图时,UIViewController中的哪个方法仅被调用一次? viewDidLoad? - Which method(s) in UIViewController is called only once, when the view is first presented? viewDidLoad? 子类化UITableViewCell中的UIProgressview需要在其他UIViewController中使用 - UIProgressview in subclassed UITableViewCell need to be used in other UIViewController viewDidLoad多久调用一次? - How often is viewDidLoad called? UIViewController需要响应来自子类UIView的事件 - UIViewController needs to respond to events from subclassed UIView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM