简体   繁体   中英

Present view controller not working on real device

I have an application that will direct user to another viewController once it is logged in.

DispatchQueue.main.async {

                    let controller = HomeViewController()
                    controller.isLoggedIn = self.loggedIn
                    controller.userRole = self.userRole
                    controller.username = self.username

                    let navigationController = UINavigationController(rootViewController: controller)

                    if #available(iOS 13.0, *) {
                        navigationController.isModalInPresentation = true
                        navigationController.modalPresentationStyle = .overFullScreen
                    } else {
                        // Fallback on earlier versions
                    }
                    print("should present here")
                    self.present(navigationController, animated: true)

            }

Above is the "redirecting" part. This code works well in the simulator, but on the real device it does not work. Any idea how I can solve it? Thanks!

Try this also Set home screen identifier in storyboard "HomeViewController"

In code:

let controller = storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController

Using.fullScreen as modalPresentationStyle should fix your issue and gives the default style used on iOS 12 or lower.

if #available(iOS 13.0, *) {
   navigationController.modalPresentationStyle = .fullScreen
} else {
   // Fallback on earlier versions
}

You need to use storyBoard reference for presenting a viewController. Which you can do by using example of the below attached code:-

let VC1 = self.storyboard.instantiateViewController(withIdentifier:"MyViewController") as! ViewController

`let navController = UINavigationController(rootViewController: VC1)` // Creating a navigation controller with VC1 at the root of the navigation stack.

self.present(navController, animated:true, completion: nil)

thanks for the response but I fixed it by

self.dismiss(animated: true) {
                        self.present(navigationController, animated: true)
                    }

apparently it didnt work as there was a error saying there was another view controller being presented ( i missed this error previously )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM