简体   繁体   English

为什么 ViewController 后退按钮在 Xcode 上不起作用?

[英]Why ViewController back buttons don't work on Xcode?

Hi I'm learning Xcode to make ios application.嗨,我正在学习 Xcode 来制作 ios 应用程序。 I have problem我有问题

This is error code -> Thread 1: "-[UIViewController back2:]: unrecognized selector sent to instance 0x14fb0e270"这是错误代码 -> 线程 1:“-[UIViewController back2:]: unrecognized selector sent to instance 0x14fb0e270”

first ViewContorller code ->第一个 ViewContorller 代码 ->

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 ->第二个 ViewContorller 代码 ->

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.每当我单击第二个视图控制器上的 back 和 back2 按钮时,我的模拟器就会停止。 And show that error code on appDelegate.并在 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?我该如何修复我的代码?

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.我认为崩溃的原因是您连接了第二个后退按钮两次,您可以在 Storyboard 的“显示连接检查器”(上方的右侧按钮)中进行检查。 Click on the button in Storyboard then click "Show the Connections Inspector" button you will see the connections in "Sent Events"单击 Storyboard 中的按钮,然后单击“Show the Connections Inspector”按钮,您将在“Sent Events”中看到连接

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")还要确保故事板场景身份检查器下的“故事板 ID”值与您在代码中使用的值(“myViewController”)相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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