从AppDelegate呈现特定的视图控制器

时间:2012-03-03 18:55:00

标签: iphone objective-c ios xcode

每次我的应用程序变为活动状态时,我都会尝试显示一个视图控制器(密码请求类型视图)。输入正确的密码后,它应该从堆栈中弹出。我试图推送的密码视图不是初始视图控制器,因此我无法从AppDelegate的applicationDidBecomeActive访问它。

我也尝试从MainViewController的ViewWillAppear中推送密码视图,但是当应用程序变为活动状态时,它不会被调用;仅在刷新屏幕时。

在过去的几天里,我一直在研究类似的问题,但我仍然不理解正确的方法。我听说过我可能无法以这种方式推送视图,因为在Storyboard或NavigationController连接/初始化/等之前可能会调用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) 

4 个答案:

答案 0 :(得分: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
}

答案 1 :(得分: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 方法中使用。

答案 2 :(得分:0)

Swift版本的快速和通用方式:

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

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

答案 3 :(得分:0)

这是Swift 4中的完整解决方案 在didFinishLaunchingWithOptions

中实现
 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

 let isLogin = UserDefaults.standard.bool(forKey: "Islogin")
    if isLogin{
        self.NextViewController(storybordid: "OtherViewController")


    }else{
        self.NextViewController(storybordid: "LoginViewController")

    }
}

在Appdelegate.swift中的任何位置编写此函数

  func NextViewController(storybordid:String)
{

    let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let exampleVC = storyBoard.instantiateViewController(withIdentifier:storybordid )
   // self.present(exampleVC, animated: true)
    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = exampleVC
    self.window?.makeKeyAndVisible()
}