swift 当我使用编程UI时,Perform Segue不工作

ukxgm1gy  于 2023-11-16  发布在  Swift
关注(0)|答案(2)|浏览(93)

我正在为我的个人项目创建一个应用程序,使用programmaticUI和UI部分的故事板,但我发现一个问题,当我试图从我的“SecondViewController”到我的“ThirdViewController”执行Segue时,我像往常一样在我的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()

    }
    
}


但每次我运行应用程序并单击登录按钮时,它总是给我这个错误:

这就是我的应用程序的样子:

我错过什么了吗?

d5vmydt9

d5vmydt91#

在这里发布问题时,最好花几分钟时间回顾一下How to Ask
但是,根据你提供的一点点信息...
几乎可以肯定的是,问题是你在故事板中添加了视图控制器,然后试图通过代码不正确地使用它们。
例如,我猜你在你的“第一个”视图控制器中有代码来加载和显示SecondViewController如下:

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

字符串
然后在SecondViewController中,你尝试使用 * 情节串联图板关联的segue*:

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


然而,这个segue并不作为SecondViewControllercode 的一部分存在,它是Storyboard对象的一部分。
回到你的第一个视图控制器,如果你像这样加载并推送到SecondViewController

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


然后你就可以调用performSegue,因为你是从情节提要加载的。

mnemlml8

mnemlml82#

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
           }
}

字符串

相关问题