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

问题描述 投票:0回答:2

我正在为我的个人项目创建一个应用程序,使用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()

    }
    
}

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

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

我在这里错过了什么吗?

ios swift segue uistoryboardsegue programmatically-created
2个回答
0
投票

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

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

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 的一部分存在……它是 Storyboard 对象的一部分。

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

SecondViewController

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

然后您将能够调用

performSegue
,因为您是从故事板加载的。


-1
投票
        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
           }
}
© www.soinside.com 2019 - 2024. All rights reserved.