本文介绍了我的所有IBOutlet在viewDidLoad中都是nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个 UIViewController ,我们可以调用 MyViewController1 。当我拨打 MyViewController1 时,中的所有 IBOutlet nil viewDidLoad (以及代码的其余部分)。

I've created a UIViewController that we can call MyViewController1. When I call MyViewController1, all my IBOutlet are nil in viewDidLoad (and in the rest of the code too).

当我通过执行
MyViewController1 * vc = [[MyViewController1 alloc] init]

如果我用另一个替换 MyViewController1 ,例如 MyViewController2 ,它有效。所以我猜问题实际上是在 MyViewController1

if I replace MyViewController1 by an other one, for example MyViewController2, it works. So I guess the problem is really in MyViewController1.

你可能想知道的最后一件事是 MyViewController1 实际上是 MySuperViewController1 的子类,它是 UIViewController

Last thing you might want to know, is that MyViewController1 is actually a subclass of MySuperViewController1 that is a UIViewController.

感谢您的帮助!

EDIT

我意识到我的情况可能更复杂。以下是我的确切文件:

I realized my case was maybe more complicated. Here are my exact files :

// MySuperViewController1

// MySuperViewController1

MySuperViewController1.h

MySuperViewController1.m

MySuperViewController1.xib

// MyViewController1

// MyViewController1

MyViewController1.h

MyViewController1.m

因此nib属于超类,而不属于子类。我可以这样做吗?

So the nib belongs to the superclass, and not the subclass. Can I do that ?

推荐答案

您应该使用:

MyViewController1 *vc = [[MyViewController1 alloc] initWithNibName:@"MyViewController1" bundle:nil]

调用 init 将不会与你的xib文件匹配,也不会分配你的不同 IBOutlet

calling init won't do the match with your xib file and won't alloc your differents IBOutlet

编辑:

有两种可能的解决方案:

There are two possibles solutions :

首先用超级nibName调用init:

First is calling init with super nibName :

MyViewController1 *vc = [[MyViewController1 alloc] initWithNibName:@"MySUperViewController1" bundle:nil]

第二个是调用超级 initWithNibName:在子init方法中

-(id)init {
   if (self = [super initWithNibName:@"MySuperViewController1" bundle:nil]) {
        // Init
   }
   return self;
}

这篇关于我的所有IBOutlet在viewDidLoad中都是nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 05:22