当我使用编程式 UI 时,Perform Segue 不起作用

发布于 2025-01-10 11:46:38 字数 6229 浏览 2 评论 0原文

我正在为我的个人项目创建一个应用程序,使用programmaticUI和故事板作为UI部分,但是当我尝试从“SecondViewController”执行Segue到“ThirdViewController”时,我发现了一个问题,我像往常一样在segue中添加了“标识符” : 输入图片描述在这里

然后我从我的 SecondViewController 中调用了“performSegue”:

import UIKit

class SecondViewController: UIViewController {
    
    private var myItem = [SecondItem]()
    lazy var myTableView : UITableView = {
       let myTable = UITableView()
        myTable.translatesAutoresizingMaskIntoConstraints = false
       return myTable
    }()

    private let myContentView : UIView = {
        let view = UIView()
        view.backgroundColor = .gray
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    lazy var label : UILabel = {
        let myLabel = UILabel()
        myLabel.text = "Hello"
        return myLabel
    }()
    
    private let unameTextField : UITextField = {
       let txtField = UITextField()
        txtField.backgroundColor = .white
        txtField.placeholder = "Username"
        txtField.borderStyle = .roundedRect
        txtField.translatesAutoresizingMaskIntoConstraints = false
       return txtField
    }()
    
    private let pwordTxtField : UITextField = {
        let txtField = UITextField()
        txtField.placeholder = "Password"
        txtField.borderStyle = .roundedRect
        txtField.translatesAutoresizingMaskIntoConstraints = false
        return txtField
    }()
    
    private let loginBtn : UIButton = {
        let btn = UIButton(type: .system)
        btn.backgroundColor = .blue
        btn.setTitle("Login", for: .normal)
        btn.tintColor = .white
        btn.layer.cornerRadius = 5
        btn.clipsToBounds = true
        btn.translatesAutoresizingMaskIntoConstraints = false
        btn.addTarget(self, action: #selector(btnPressed), for: .touchUpInside)
        return btn
    }()
    
//I called the "performSegue" here
    @objc func btnPressed() {
        performSegue(withIdentifier: "gotoBla", sender: self)
        print("button pressed")
    }
    
    lazy var imageView : UIImageView = {
       let image = UIImage(named: "image_4")
       let imageView = UIImageView(image: image)
       imageView.translatesAutoresizingMaskIntoConstraints = false
       return imageView
    }()
    
    
    
    
    func setAutoLayout(){
        
        let guide = view.safeAreaLayoutGuide
   
        myContentView.anchor(top: guide.topAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: view.frame.height / 3, enableInsets: true)
        
        imageView.anchor(top: myContentView.topAnchor, left: nil , bottom: nil , right: nil , paddingTop: 10, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 80, height: 80, enableInsets: true)
        imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

        unameTextField.anchor(top: imageView.bottomAnchor, left: myContentView.leftAnchor, bottom: nil, right: myContentView.rightAnchor, paddingTop: 10, paddingLeft: 20, paddingBottom: 5, paddingRight: 20, width: 0, height: 40, enableInsets: true)
        
        pwordTxtField.anchor(top: unameTextField.bottomAnchor, left: myContentView.leftAnchor, bottom: nil, right: myContentView.rightAnchor, paddingTop: 30, paddingLeft: 20, paddingBottom: 0, paddingRight: 20, width: 0, height: 40, enableInsets: true)
        
        loginBtn.anchor(top: pwordTxtField.bottomAnchor, left: myContentView.leftAnchor, bottom: nil, right: myContentView.rightAnchor , paddingTop: 20, paddingLeft: 20, paddingBottom: 0, paddingRight: 20, width: 0, height: 40, enableInsets: true)

        //TableView
        myTableView.topAnchor.constraint(equalTo: myContentView.bottomAnchor).isActive = true
        myTableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        myTableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        myTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        myItem.append(SecondItem(text: "first"))
        myItem.append(SecondItem(text: "Second"))
        myItem.append(SecondItem(text: "Third"))

        
        view.backgroundColor = .white
        
        view.addSubview(myContentView)
        myContentView.addSubview(unameTextField)
        myContentView.addSubview(pwordTxtField)
        myContentView.addSubview(loginBtn)
        myContentView.addSubview(imageView)

        myTableView.register(SecondTableViewCell.self, forCellReuseIdentifier: K.SecondTableViewCell.identifier)
        myTableView.delegate = self
        myTableView.dataSource = self

        view.addSubview(myTableView)
        
        setAutoLayout()

        
    }
}

extension SecondViewController : UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(indexPath.row)
    }
}

extension SecondViewController : UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        myItem.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: K.SecondTableViewCell.identifier, for: indexPath) as! SecondTableViewCell
        cell.second = myItem[indexPath.row]
        cell.selectionStyle = .none
        
        return cell
    }
}

对于第三个视图控制器,我还没有在其中添加一些代码

import UIKit

class ThirdViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }
    
}

但是每次我运行应用程序并单击登录按钮时,它总是给我这个 错误: 输入图片此处描述

这就是我的应用程序的样子: 输入图片这里的描述

我在这里错过了什么吗?

I am creating an app for my personal project using programmaticUI and storyboard for the UI part, but I found an issue when I tried to performSegue from my "SecondViewController" to my "ThirdViewController", I added the "identifier" in my segue like usual:
enter image description here

And then I called the "performSegue" from my SecondViewController:

import UIKit

class SecondViewController: UIViewController {
    
    private var myItem = [SecondItem]()
    lazy var myTableView : UITableView = {
       let myTable = UITableView()
        myTable.translatesAutoresizingMaskIntoConstraints = false
       return myTable
    }()

    private let myContentView : UIView = {
        let view = UIView()
        view.backgroundColor = .gray
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    lazy var label : UILabel = {
        let myLabel = UILabel()
        myLabel.text = "Hello"
        return myLabel
    }()
    
    private let unameTextField : UITextField = {
       let txtField = UITextField()
        txtField.backgroundColor = .white
        txtField.placeholder = "Username"
        txtField.borderStyle = .roundedRect
        txtField.translatesAutoresizingMaskIntoConstraints = false
       return txtField
    }()
    
    private let pwordTxtField : UITextField = {
        let txtField = UITextField()
        txtField.placeholder = "Password"
        txtField.borderStyle = .roundedRect
        txtField.translatesAutoresizingMaskIntoConstraints = false
        return txtField
    }()
    
    private let loginBtn : UIButton = {
        let btn = UIButton(type: .system)
        btn.backgroundColor = .blue
        btn.setTitle("Login", for: .normal)
        btn.tintColor = .white
        btn.layer.cornerRadius = 5
        btn.clipsToBounds = true
        btn.translatesAutoresizingMaskIntoConstraints = false
        btn.addTarget(self, action: #selector(btnPressed), for: .touchUpInside)
        return btn
    }()
    
//I called the "performSegue" here
    @objc func btnPressed() {
        performSegue(withIdentifier: "gotoBla", sender: self)
        print("button pressed")
    }
    
    lazy var imageView : UIImageView = {
       let image = UIImage(named: "image_4")
       let imageView = UIImageView(image: image)
       imageView.translatesAutoresizingMaskIntoConstraints = false
       return imageView
    }()
    
    
    
    
    func setAutoLayout(){
        
        let guide = view.safeAreaLayoutGuide
   
        myContentView.anchor(top: guide.topAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: view.frame.height / 3, enableInsets: true)
        
        imageView.anchor(top: myContentView.topAnchor, left: nil , bottom: nil , right: nil , paddingTop: 10, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 80, height: 80, enableInsets: true)
        imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

        unameTextField.anchor(top: imageView.bottomAnchor, left: myContentView.leftAnchor, bottom: nil, right: myContentView.rightAnchor, paddingTop: 10, paddingLeft: 20, paddingBottom: 5, paddingRight: 20, width: 0, height: 40, enableInsets: true)
        
        pwordTxtField.anchor(top: unameTextField.bottomAnchor, left: myContentView.leftAnchor, bottom: nil, right: myContentView.rightAnchor, paddingTop: 30, paddingLeft: 20, paddingBottom: 0, paddingRight: 20, width: 0, height: 40, enableInsets: true)
        
        loginBtn.anchor(top: pwordTxtField.bottomAnchor, left: myContentView.leftAnchor, bottom: nil, right: myContentView.rightAnchor , paddingTop: 20, paddingLeft: 20, paddingBottom: 0, paddingRight: 20, width: 0, height: 40, enableInsets: true)

        //TableView
        myTableView.topAnchor.constraint(equalTo: myContentView.bottomAnchor).isActive = true
        myTableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        myTableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        myTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        myItem.append(SecondItem(text: "first"))
        myItem.append(SecondItem(text: "Second"))
        myItem.append(SecondItem(text: "Third"))

        
        view.backgroundColor = .white
        
        view.addSubview(myContentView)
        myContentView.addSubview(unameTextField)
        myContentView.addSubview(pwordTxtField)
        myContentView.addSubview(loginBtn)
        myContentView.addSubview(imageView)

        myTableView.register(SecondTableViewCell.self, forCellReuseIdentifier: K.SecondTableViewCell.identifier)
        myTableView.delegate = self
        myTableView.dataSource = self

        view.addSubview(myTableView)
        
        setAutoLayout()

        
    }
}

extension SecondViewController : UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(indexPath.row)
    }
}

extension SecondViewController : UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        myItem.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: K.SecondTableViewCell.identifier, for: indexPath) as! SecondTableViewCell
        cell.second = myItem[indexPath.row]
        cell.selectionStyle = .none
        
        return cell
    }
}

And for the Third View Controller, i am not yet adding some code in there

import UIKit

class ThirdViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }
    
}

But every time I run the app and click the login button, it always gave me this error:
enter image description here

This is what my app looks like:
enter image description here

Do i miss something here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愚人国度 2025-01-17 11:46:38

几乎可以肯定,问题在于您在 Storyboard 中添加视图控制器,然后不正确地尝试通过代码使用它们。

例如,我猜测您的“第一个”视图控制器中有代码来加载和显示 SecondViewController ,如下所示:

@objc func showSecondTapped(_ sender: Any) {
    let vc = SecondViewController()
    navigationController?.pushViewController(vc, animated: true)
}

然后在 SecondViewController 中您尝试使用Storyboard 关联了 segue

@objc func btnPressed(_ sender: Any) {
    performSegue(withIdentifier: "gotoBla", sender: self)
    print("button pressed")
}

但是,该 segue 并不作为 SecondViewController code 的一部分存在。它是故事板的一部分。 目的。

回到您的第一个视图控制器,如果您像这样加载并推送到 SecondViewController

@objc func showSecondTapped(_ sender: Any) {
    if let vc = storyboard?.instantiateViewController(withIdentifier: "secondVC") as? SecondViewController {
        navigationController?.pushViewController(vc, animated: true)
    }
}

您将能够调用 performSegue 因为您是从 Storyboard 加载它的。

Almost certainly the problem is that you are adding View Controllers in Storyboard and then improperly trying to use them via code.

For example, I'm guessing that you have code in your "first" view controller to load and display SecondViewController like this:

@objc func showSecondTapped(_ sender: Any) {
    let vc = SecondViewController()
    navigationController?.pushViewController(vc, animated: true)
}

and then in SecondViewController you're trying to use the Storyboard associated segue with this:

@objc func btnPressed(_ sender: Any) {
    performSegue(withIdentifier: "gotoBla", sender: self)
    print("button pressed")
}

However, that segue doesn't exist as part of SecondViewController code ... it is part of the Storyboard object.

Back in your first view controller, if you load and push to SecondViewController like this:

@objc func showSecondTapped(_ sender: Any) {
    if let vc = storyboard?.instantiateViewController(withIdentifier: "secondVC") as? SecondViewController {
        navigationController?.pushViewController(vc, animated: true)
    }
}

you will then be able to call performSegue because you loaded it from the Storyboard.

暮年 2025-01-17 11:46:38
        if let vc = storyboard?.instantiateViewController(withIdentifier: "secondVC") as? SecondViewController {
            
        navigationController?.pushViewController(vc, animated: true)


         // if navigationController?.pushViewController doesn't work you can try this
        present(vc, animated: true) {
            // anything you want to perform after presenting new screen
        }
// and try this to show in full screen with different transition effect
        
        vc.modalTransitionStyle = .crossDissolve
        vc.modalPresentationStyle = .fullScreen
        
        present(vc, animated: true) {
            // anything you want to perform after presenting new screen
           }
}
        if let vc = storyboard?.instantiateViewController(withIdentifier: "secondVC") as? SecondViewController {
            
        navigationController?.pushViewController(vc, animated: true)


         // if navigationController?.pushViewController doesn't work you can try this
        present(vc, animated: true) {
            // anything you want to perform after presenting new screen
        }
// and try this to show in full screen with different transition effect
        
        vc.modalTransitionStyle = .crossDissolve
        vc.modalPresentationStyle = .fullScreen
        
        present(vc, animated: true) {
            // anything you want to perform after presenting new screen
           }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文