相关疑难解决方法(0)

如何获得root视图控制器?

我需要一个根视图控制器的实例.

我试过这些方法:

UIViewController *rootViewController = (UIViewController*)[[[UIApplication sharedApplication] keyWindow] rootViewController];
Run Code Online (Sandbox Code Playgroud)

返回: null:

当我尝试获得一组控制器时:

NSArray *viewControllers = self.navigationController.viewControllers;
Run Code Online (Sandbox Code Playgroud)

它只返回一个控制器,但它不是我的根视图控制器.

如果我尝试从导航控制器:

UIViewController *root = (UIViewController*)[self.navigationController.viewControllers objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)

返回: null:

有什么想法吗?还有什么我可以尝试获取我的根视图控制器的实例?

谢谢.

objective-c uiviewcontroller uinavigationcontroller ios5 swift

101
推荐指数
7
解决办法
11万
查看次数

如何从appDelegate呈现UIAlertView

当应用程序收到推送通知时,我试图在didReceiveRemoteNotification中显示来自appDelegate的UIAlertView.

我有这个错误:

  Warning: Attempt to present <UIAlertController: 0x14c5494c0> on <UINavigationController:
  0x14c60ce00> whose view is not in the window hierarchy!
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: NSDictionary) {

    var contentPush: NSDictionary = userInfo.objectForKey("aps") as NSDictionary

    var message = contentPush.objectForKey("alert") as String



    let alertController = UIAlertController(title: "Default Style", message: message, preferredStyle: .Alert)

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
                    // ...
    }
    alertController.addAction(cancelAction)

    let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in

        let photoPushedVc = self.storyboard.instantiateViewControllerWithIdentifier("CommentTableViewController") as CommentTableViewController …
Run Code Online (Sandbox Code Playgroud)

view ios swift

14
推荐指数
5
解决办法
2万
查看次数