2012-03-03 55 views
7

我試圖提出一個視圖控制器(密碼請求類型視圖),每次我的應用程序變爲活動狀態。一旦輸入正確的密碼,它應該彈出堆棧。我試圖推送的密碼視圖不是初始視圖控制器,所以我無法通過AppDelegate的applicationDidBecomeActive訪問它。從AppDelegate呈現特定的視圖控制器

我也嘗試從我的MainViewController的ViewWillAppear推送密碼視圖,但它不會在應用程序變爲活動狀態時被調用;只有當屏幕刷新時纔會顯示。

過去幾天我一直在研究類似的問題,但我仍然不理解正確的方法。我聽說我可能無法以這種方式推送視圖,因爲可能會在Storyboard或NavigationController連接/初始化/ etc之前調用applicationDidBecomeActive。

如果任何人都可以提供正確的方式來推送/呈現視圖,或者如果有其他地方會更好地做這種事情,我會很感激。

已解決:我剛剛從故事板中刪除了視圖,而是使用了筆尖。我用:

PasscodeUnlockVC *passcodeUnlock = [[PasscodeUnlockVC alloc] initWithNibName:@"PasscodeUnlockVC" bundle:[NSBundle mainBundle]]; 
[(UINavigationController *)self.window.rootViewController pushViewController:passcodeUnlock animated:NO]; 

景觀層次數據:

(gdb) po [[(id)UIApp keyWindow] recursiveDescription] 
<UIWindow: 0x736a4b0; frame = (0 0; 320 480); layer = <UIWindowLayer: 0x7367b40>> 
    | <UILayoutContainerView: 0x76977a0; frame = (0 0; 320 480); autoresize = W+H; layer =  <CALayer: 0x769de60>> 
    | | <UINavigationTransitionView: 0x7692110; frame = (0 0; 320 480); clipsToBounds = YES;  autoresize = W+H; layer = <CALayer: 0x769a8c0>> 
    | | | <UIViewControllerWrapperView: 0x73868d0; frame = (0 64; 320 416); autoresize = W+H; layer = <CALayer: 0x75510e0>> 
    | | | | <UIView: 0x76b93e0; frame = (0 0; 320 416); autoresize = W+H; layer = <CALayer: 0x7386850>> 
    | | <UINavigationBar: 0x73664b0; frame = (0 20; 320 44); clipsToBounds = YES; opaque = NO; autoresize = W; layer = <CALayer: 0x7366550>> 
    | | | <UINavigationBarBackground: 0x7360ea0; frame = (0 0; 320 44); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7366520>> - (null) 
    | | | <UINavigationItemView: 0x76b95e0; frame = (160 21; 0 0); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x76aa8e0>> 
    | | | <UINavigationItemButtonView: 0x7550650; frame = (5 7; 73 30); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7368b40>> 
Current language: auto; currently objective-c 
(gdb) 
+0

我不熟悉的'viewDidBecomeActive'消息,當我搜索'developer.apple.com'吧,我沒有得到結果。也許你應該告訴我們更多關於這種方法,或者甚至向我們展示你的代碼。 – 2012-03-03 18:59:30

+0

對不起,我的意思是applicationDidBecomeActive。我將編輯帖子。 – 2012-03-03 19:08:16

+0

我試圖複製你的模態視圖顯示,但它告訴我pushViewController:動畫是無法識別的,你能告訴我,如果你在iOS 6上嘗試過嗎? – perrohunter 2012-11-12 22:24:23

回答

4

我建議你使用applicationWillEnterForeground:,不applicationDidBecomeActive:,因爲它的工作原理與多任務手勢更好。例如,如果用戶雙擊主頁按鈕以顯示任務欄,則不需要放置鎖定屏幕,然後在不更改應用程序的情況下關閉任務欄。

假設您的AppDelegate擁有window屬性,並且您知道您的窗口的根視圖控制器是UINavigationController

- (void)applicationWillEnterForeground:(UIApplication *)application { 
    PasscodeViewController *pvc = [[PasscodeViewController alloc] init]; 
    [(UINavigationController *)self.window.rootViewController pushViewController:pvc animated:NO]; 
    // [pvc release]; if not using ARC 
} 
+0

我在沒有任何警告或錯誤的情況下實現了該代碼,但不是提出整個視圖,而是在其下面顯示帶有黑色屏幕的導航欄。有任何想法嗎?我不必在頭文件中聲明視圖或任何事情?再次感謝。 – 2012-03-03 19:41:06

+0

在調試器中暫停並執行'po [[(id)UIApp keyWindow] recursiveDescription]'。 – 2012-03-03 19:42:38

+0

好的,我得到了有關視圖層次結構的信息,但我不確定如何解析/使用它。SO上的其他人也遇到了與黑屏相同的問題,解決方案是刪除loadView實現,這是我沒有的。 – 2012-03-03 20:08:04

0

解決

PasscodeViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"passcodeVCID"]; 
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:vc]; 
navController.navigationBar.hidden = YES; 
vc.mode = @"enable"; 
self.window.rootViewController = navController; 

使用在applicationWillEnterForeground方法。

0

斯威夫特版本快速和廣義方式:

getRootViewController().presentViewController(messageVC, animated: true, completion: nil) 

func getRootViewController() -> UIViewController 
{ 
    return (UIApplication.sharedApplication().delegate?.window??.rootViewController)! 
} 
相關問題