2012-03-13 20 views
4

這是情況。我在Appdelegate.m中有一個NSTimer,它執行一個方法來檢查用戶的位置,如果他不在某個區域,就會將其記錄下來。當發生這種情況時,我想加載登錄視圖控制器。但是,通過以下幾行代碼,我可以看到VC,但是如果我點擊一個文本框來鍵入,我會得到一個sigbart。任何想法如何從應用程序委託正確加載初始VC?如何從應用程序代理加載登錄視圖控制器

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil]; 
      LoginViewController *loginViewController = (LoginViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"Login"]; 
      [self.window addSubview:loginViewController.view]; 
      [self.window makeKeyAndVisible]; 
+0

如果您發現了這些問題的答案之一是正確的,你會介意將其標記爲正確 – 2013-01-01 21:44:00

回答

1

這時候我想改變我的看法使用的代碼:

#import "LoginViewController.h" 

LoginViewController *screen = [[LoginViewController alloc] initWithNibName:nil bundle:nil]; 
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
[self presentModalViewController:screen animated:YES]; 

//or 

[self.window addSubView:screen]; 

使用該代碼將創建它的一個全新的實例,並表現出來,所以要做到你的問題。你可能需要移動一些東西,但它應該起作用。

讓我知道你是否需要任何幫助。

希望這可以幫助你!

+0

大衛,它不起作用。如果我按照你說的方式初始化登錄VC,什麼都不會發生。 – audiophilic 2012-03-14 01:38:26

+0

是否有任何錯誤消息 – David 2012-03-14 18:07:23

11

我一直在努力與此相當。這裏是什麼對我來說

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil]; 
LoginViewController *loginViewController = (LoginViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"Login"]; 

//set the root controller to it 
self.window.rootViewController = loginViewController 

如果你仍然有錯誤,它必須與你的LoginViewController,而不是AppDelegate過渡。希望這可以幫助。

+0

我不認爲您可以將VC分配給self.window.rootViewController。 – Dean 2014-08-06 02:47:35

4

audiophilic和Flaviu的方法都可以。但是你們都錯過了一行額外的代碼。

我發現你必須先分配和初始化UIWindow對象。我不知道爲什麼,但初始化對象解決了問題。

self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

audiophilic的做法:

self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil]; 

ResultsInListViewController *resultsInListViewController = (ResultsInListViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"ResultsInListViewController"]; 

[self.window addSubview:resultsInListViewController.view]; 

[self.window makeKeyAndVisible]; 

Flaviu的做法:

self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil]; 

ResultsInListViewController *resultsInListViewController = (ResultsInListViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"ResultsInListViewController"]; 

[self.window setRootViewController:resultsInListViewController]; 

[self.window makeKeyAndVisible]; 
相關問題