2012-08-07 77 views
0

我有這樣的級聯:iPhone - self.view是initWithNibName子零,而viewDidLoad中不叫

某處在應用

- (void) go 
{ 
    MyCustomViewController* controller = [[MyCustomViewController alloc] init]; 
    controller.delegate = self; 
    [controller run]; 
} 

在MyCustomViewController

- (id) init 
{ 
    // there is an if statement here in real to choose another XIB if needed 
    // but I only display here the code that is called in my tests 
    self = [super initWithNibName:@"MyXIB" bundle:nil]; 
    if (!self) return nil; 

    self.delegate = nil; 

    return self; 
} 

- (void) run 
{ 
    // do things with self.view 
} 

MyCustomViewController繼承GenericWindowController

在GenericWindowController

/*- (id) initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil 
{ 
    if (!(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) return nil; 

    self.view.userInteractionEnabled = NO; // THE APP CRASHES HERE ! self.view is nil 

    ... 

    return self; 
}*/ 

// METHOD created following first answers : NOT CALLED 
- (void) viewDidLoad 
{ 
    self.view.userInteractionEnabled = NO; 
    // and many other things done with self.view 
} 

MyXIB 有文件的所有者設置爲MyCustomViewController和視圖連接。
包含所有文件並將其檢入到項目中。

GenericWindowController被設計來製作一些標準的東西。
MyCustomViewController將這些東西擴展到MyXIB中設計的自定義視圖。

爲什麼self.view在GenericWindowController中爲零?
爲什麼viewDidLoad不叫?

回答

0

不可思議!!!問題是因爲XIB缺少本地化。增加了本地化,並且沒有問題了。

+0

????請解釋 – 2014-12-07 03:36:35

+0

@TylerPfaff:我的應用程序已本地化,並且其中一個本地化文件(即使由於設備配置而未使用)丟失。我只是把它加回來,沒有更多的問題。 – Oliver 2014-12-07 11:09:35

2

self.view僅在viewDidLoad之後有效 - 在init...之內,它仍然是nil

2

視圖控制器不應該嘗試訪問其在initWithNibName:bundle:中的視圖。它應該等到viewDidLoad

+0

謝謝,我錯過了它,因爲使用了舊的代碼正在工作......奇怪。的確,viewDidLoad不會被調用,既不在超級也不在子類中... – Oliver 2012-08-07 21:58:43

+0

我編輯了這個問題。如何在運行被調用時確保視圖被引用?我的意思是,在viewDidLoad中運行的代碼(應該只運行一次)不會在這種方法中調用,因爲它沒有被調用。我該怎麼做? – Oliver 2012-08-07 22:16:39

+0

您需要閱讀[瞭解視圖如何加載和卸載](http://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html#//apple_ref/doc/uid/TP40007457-CH10 -SW2)。 – 2012-08-07 22:18:29