2012-11-23 29 views
0

在loggerViewController.m:遞歸調用viewDidLoad中,轟然

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UIView* mainView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
    [self.view addSubview:mainView]; // <-- Problem is here 
} 

loggingViewController是我的appDelegate的伊娃

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    . 
    . 
    loggingViewController = [[loggerViewController alloc] init]; 
    [loggingViewController.view setBackgroundColor:[UIColor blueColor]]; 
// [loggingViewController loadView]; 
    [self.view addSubview:loggingViewController.view]; 

} 

我期待我的AppDelegate調用loggingViewController,反過來,在裏面設置它自己的子視圖,這將完成。但相反,viewDidLoad被遞歸調用,我不明白爲什麼?

+0

遞歸調用的原因是你的'self.view'是'nil',因此它試圖一次又一次地調用。你是否在你的loadView方法中做了其他的事情?如果你只是註釋'[loggingViewController.view setBackgroundColor:[UIColor blueColor]];',是否發生? – iDev

+0

是的,我現在注意到AppDelegate的視圖從未初始化。但是當我像'loggingViewController.view = [[UIView alloc] initWithFrame:rect]一樣初始化它時,''viewDidLoad'永遠不會被調用! – Ted

+0

爲什麼從未初始化?你能找到什麼特殊原因?你不應該'loggingViewController.view = [[UIView alloc] initWithFrame:rect];'這會產生更多的問題。 – iDev

回答

1

嘗試像這樣,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
    loggingViewController = [[loggerViewController alloc] init]; 
    self.window.rootViewController = loggingViewController; 
    [self.window makeKeyAndVisible]; 
} 

原因遞歸調用的是,你的self.viewnil,因此它是嘗試,當你試圖將其添加爲的appdelegate的視圖的子視圖一次又一次打電話。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UIView* mainView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
    [self.view setBackgroundColor:[UIColor blueColor]]; 
    [self.view addSubview:mainView]; 
} 
+0

它有點複雜,我有一個選項卡欄,我想把這個特定的視圖放在另一個視圖的頂部。如何設置控制器和視圖以便設置所有子視圖? – Ted

+0

如何像'[self.tabController presentModalViewController:loggingViewController animated:NO];'?你也可以檢查這個,http://stackoverflow.com/questions/4406426/adding-login-screen-in-front-of-cocoa-touch-tab-bar-application-for-ios – iDev

+0

我沒有看到你在哪裏初始化你的視圖順便說一句。我不明白我應該如何初始化它。該例中的 – Ted