skip to Main Content

Hi I’m learning Xcode to make ios application. I have problem

This is error code ->
Thread 1: "-[UIViewController back2:]: unrecognized selector sent to instance 0x14fb0e270"

first ViewContorller code ->

import UIKit

class ViewController: UIViewController {


    @IBAction func moveByNavi(_ sender: Any) {
        guard let uvc = self.storyboard?.instantiateViewController(withIdentifier: "SecondVC") else {
            return
        }
        
        self.navigationController?.pushViewController(uvc, animated: true)
    }
    
    @IBAction func movePresent(_ sender: Any) {
        guard let uvc = self.storyboard?.instantiateViewController(withIdentifier: "SecondVC") else {
            return
        }
        
        uvc.modalPresentationStyle = .fullScreen
        self.present(uvc, animated: true)
    }
}

Second ViewContorller code ->

import UIKit

class SecondViewController: UIViewController {
    
    @IBAction func back(_ sender: Any) {
        self.presentingViewController?.dismiss(animated: true)
    }
    
    @IBAction func back2(_ sender: Any) {
        _ = self.navigationController?.popViewController(animated: true)
    }
}

Whenever I click back and back2 buttons on second view controller, My simulator stop. And show that error code on appDelegate.

I Checked connections inspector of second view Controller. back buttons connection is connected well. I don’t have rests of connection as well.
How can I fix my code?

2

Answers


  1. Make sure you instantiate the view controller using:

    storyboardInstance.instantiateViewController(withIdentifier: "myViewController")
    

    Also make sure that the "Storyboard ID" value under the identity inspector of your storyboard scene is identical to the one you use on code ("myViewController")

    Login or Signup to reply.
  2. I think the reason for the crash is that you connected your second back button twice, you can check that in "Show the Connections Inspector"(right button above) in Storyboard. Click on the button in Storyboard then click "Show the Connections Inspector" button you will see the connections in "Sent Events"

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search