Swift - pushViewController from appDelegate, rootViewController.navigationController is nil

前端 未结 7 719
無奈伤痛
無奈伤痛 2020-12-08 14:57

Having a problem following a few guides, specifically http://blog.originate.com/blog/2014/04/22/deeplinking-in-ios/

I\'m setting the url scheme and it\'s working wel

相关标签:
7条回答
  • 2020-12-08 15:08

    Swift 3 & 4 best practices to push viewcontroller from AppDelegate:

    if let rootViewController = self.window!.rootViewController as? UINavigationController {
       let storyboard = UIStoryboard(name: "Main", bundle: nil)
    
       if let viewcontroller = storyboard.instantiateViewController(withIdentifier: "DiscussionBoardSID") as? DiscussionBoardViewController {
          viewcontroller.postID = "13" ///pass data to your viewcontroller
          rootViewController.pushViewController(viewcontroller, animated: true)
       }
    }
    
    0 讨论(0)
  • 2020-12-08 15:09
    func pushNewView() {
        if let wind = UIApplication.sharedApplication().delegate?.window {
            if let rootViewController = wind?.rootViewController {
                let viewToPush = YourViewController()
                let nav1 = UINavigationController(rootViewController: viewToPush)
                if let alreadySomeOneThere = rootViewController.presentedViewController {
                    alreadySomeOneThere.presentViewController(nav1, animated: true, completion: nil)
                }else {
                    rootViewController.presentViewController(nav1, animated: true, completion: nil)
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-08 15:12

    APPDELEGATE TO PAGE:

            let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let loginPageView = mainStoryboard.instantiateViewControllerWithIdentifier("leadBidderPagerID") as! LeadBidderPage
            var rootViewController = self.window!.rootViewController as! UINavigationController
            rootViewController.pushViewController(loginPageView, animated: true)
    

    PAGE TO PAGE:

            let loginPageView = self.storyboard?.instantiateViewControllerWithIdentifier("scoutPageID") as! ScoutPage
            self.navigationController?.pushViewController(loginPageView, animated: true)
    
    0 讨论(0)
  • 2020-12-08 15:18

    If you want to present and dismiss with NavigationBar or Dismiss(Animated: true, completion: nil) Set addObserver in didFinishLaunchingWithOptions

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
    
    
    NotificationCenter.default.addObserver(self, selector: #selector(devolucionNoPresencial), name: NSNotification.Name(rawValue: "DevolucionNoPresencial"), object: nil)
            return true
    }
    

    then in your method

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    //do your code for .active, .inactive .background, etc
    
     if let userInfo = response.notification.request.content.userInfo as? [String : AnyObject] {
                if UIApplication.shared.applicationState == .active {
    //activate the notificationCenter so you can call your @objc method
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "DevolucionNoPresencial"), object: nil)
    }
    

    add @objc method

    @objc private func devolucionNoPresencial() {
    
    
           //change DevolucionVC for VC you want to present
    
            if let controller = DevolucionVC() as? DevolucionVC {
                if let window = self.window, let rootViewController = window.rootViewController {
                    var currentController = rootViewController
                    while let presentedController = currentController.presentedViewController {
                        currentController = presentedController
                    }
                    currentController.present(controller, animated: true, completion: nil)
                }
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-08 15:19

    It seems that rootViewController actually is of type UINavigationController in my case, so casting it on declaration allowed me to call pushToViewController directly on it.

    func application(application: UIApplication, openURL url: NSURL, sourceApplication: String, annotation: AnyObject?) -> Bool {
        let rootViewController = self.window!.rootViewController as! UINavigationController
        let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let profileViewController = mainStoryboard.instantiateViewController(withIdentifier: "InstructionVC") as! InstructionVC
        rootViewController.pushViewController(profileViewController, animated: true)
        return true
    
    }
    
    0 讨论(0)
  • 2020-12-08 15:29

    SWIFT 4: Safe way to push with the use of conditional binding and Chaining

    if let navController = self.navigationController, let viewController = self.storyboard?.instantiateViewController(withIdentifier: "indentfier") as? CustomViewController{
        navController.pushViewController(viewController, animated: true)
    }
    

    In One line of code :

    Swift 3:

    self.navigationController!.pushViewController(self.storyboar‌​d!.instantiateViewCo‌​ntroller(withIdentif‌​ier: "view2") as UIViewController, animated: true)
    

    self.navigationController!.pushViewController(self.storyboard!.instantiateViewControllerWithIdentifier("view2") as UIViewController, animated: true)
    
    0 讨论(0)
提交回复
热议问题