Home > database >  Crashing when I use navigation bar
Crashing when I use navigation bar

Time:12-09

I'm trying to add a navigation bar to jump from my main view to a second view but I have two problems.

First: the second view is not open at full screen. It's just floating over the first one.
Second: every time I push the back button, which I've change its text to "volver", it crashes.

I've created a function I call when I press a button in the first viewController. My code is below and as you can check I've tried with dismiss, popViewController, and popToRoot but nothing works. I've given the id "RegistroViewController" to my second view.

@IBAction func registrarse(_ sender: Any) {
    if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "RegistroViewController") {
        viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Volver", style: UIBarButtonItem.Style.plain, target: self, action: #selector(UIWebView.goBack))
        let navController = UINavigationController(rootViewController: viewController)
        self.present(navController, animated:true, completion: nil)
    }
}

func goBack(){
    //self.navigationController?.popViewController(animated: true)
    dismiss(animated: true, completion: nil)
    //self.navigationController?.popToRootViewController(animated: true)
}

The error is:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[eventos.AuthViewController goBack]: unrecognized selector sent to instance 0x7ff192c09a80' terminating with uncaught exception of type NSException CoreSimulator 732.17 - Device: iPhone 8 (5334009E-7A5B-4420-A4AD-EC91CB881356) - Runtime: iOS 14.0 (18A372) - DeviceType: iPhone 8

CodePudding user response:

first you have set modelPresentationStyle of a controller it will show as full screen but will present from bottom to top if you want to change transition style I will also write

let navController = UINavigationController(rootViewController: viewController)
navController.modalPresentationStyle = .overFullScreen // show full screen
// navController.modalTransitionStyle = .crossDissolve // choose whatever
self.present(navController, animated:true, completion: nil)

second the crash is due to the selector function

UIBarButtonItem(title: "Volver", style: UIBarButtonItem.Style.plain, target: self, action: #selector(UIWebView.goBack))
        

the goBack function must be on RegistroViewController and you can access is

UIBarButtonItem(title: "Volver", style: UIBarButtonItem.Style.plain, target: self, action: #selector(viewController.goBack))
  • Related