“Presenting view controllers on detached view cont

2019-06-13 13:57发布

问题:

I have a navigation controller and root view. Then in that root view, I pushed a view there I have to click to watch the content, if logged in, if not logged it, it would open a modal view controller with the below code.

        var vc = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
        self.tabBarController!.presentViewController(vc, animated: true, completion: nil)

In viewdidload() i have a nsnotification so that I could get notified when user loggedinback from login modal

 NSNotificationCenter.defaultCenter().addObserver(self, selector: ("didDismissSecondViewController:"), name: "SecondViewControllerDismissed", object: nil)

In Login controller after all work I wrote the below code

 self.dismissViewControllerAnimated(true, completion: {NSNotificationCenter.defaultCenter().postNotificationName("SecondViewControllerDismissed", object: nil, userInfo: nil)});

So in my first view controller I wrote the below code

func didDismissSecondViewController(sender: AnyObject)
{
    NSLog("Called ns notifications")
    dispatch_async(dispatch_get_main_queue(),{

        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        if Reachability.isConnectedToNetwork() == true {
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc : VideoPlayViewController = storyboard.instantiateViewControllerWithIdentifier("video") as! VideoPlayViewController
        vc.movieUrl = self.movieURL
        NSLog("URL:- \(self.movieURL)")
        let navigationController = UINavigationController(rootViewController: vc)

        self.presentViewController(navigationController, animated: true, completion: nil)

        }
        else
        {

         var alert = UIAlertView(title: "No Internet connection", message: "Please ensure you are connected to the Internet. Connect to internet and and choose any sorting option to reload.", delegate: self,  cancelButtonTitle: "Cancel")
                alert.addButtonWithTitle("Ok")
                alert.show()


        }


    })

}

However, when it tries to present the view controller after dismiss login controller, it shows "Presenting view controllers on detached view controllers is discouraged" Error and it crashed.

回答1:

Thank God I found the answer!!!

var viewCon:UIViewController = self.presentingViewController!

self.dismissViewControllerAnimated(true, completion: {let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

let vc : VideoPlayViewController = storyboard.instantiateViewControllerWithIdentifier("video") as! VideoPlayViewController

vc.movieUrl = self.movieUrl

let navigationController = UINavigationController(rootViewController: vc)

viewCon.presentViewController(navigationController, animated: true, completion: nil)});