Swift 以编程方式创建segue对象

Swift 以编程方式创建segue对象,swift,Swift,我创建了一个带有2个视图控制器的单视图应用程序。在第一个视图控制器中,我有一个标签和两个按钮,它们是通过编程创建的。从这些按钮到第二个视图控制器,我有两个分段。我希望每个按钮都能为第二视图控制器提供不同的信息。通过以编程方式创建这些按钮和分段,我无法将标识符分配给分段。因此,我不能使用performsgue(带有标识符:sender:)。 我该怎么做 第一个视图控制器: import UIKit class ViewController: UIViewController { var

我创建了一个带有2个视图控制器的单视图应用程序。在第一个视图控制器中,我有一个标签和两个按钮,它们是通过编程创建的。从这些按钮到第二个视图控制器,我有两个分段。我希望每个按钮都能为第二视图控制器提供不同的信息。通过以编程方式创建这些按钮和分段,我无法将标识符分配给分段。因此,我不能使用
performsgue(带有标识符:sender:)
。 我该怎么做

第一个视图控制器:

import UIKit

class ViewController: UIViewController {
    var myLabel: UILabel!
    var leftButton: UIButton!
    var rightButton: UIButton!

// bellow are the 2 variable which I’d like to transport into the second ViewController

    var leftButtonText = "I'm the left button."
    var rightButtonText = "I'm the right button."

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let homeNavBar = UINavigationBar()
        homeNavBar.frame = CGRect(x: 0, y: 0, width: 320, height: 45)
        homeNavBar.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(homeNavBar)

        let homeNavItem = UINavigationItem(title: "Home")
        homeNavBar.setItems([homeNavItem], animated: false)


        myLabel = UILabel()
        myLabel.frame = CGRect(x: 165, y: 150, width: 200, height: 42)
        myLabel.text = "Press one of the button bellow!"

        self.view.addSubview(myLabel)

        leftButton = UIButton(type: .custom)
        leftButton.frame = CGRect(x: 80, y: 300, width: 70, height: 70)
        leftButton.backgroundColor = UIColor.cyan
        leftButton.setTitleColor(UIColor.black, for: .normal)
        leftButton.setTitle("Left Button", for: .normal)
        leftButton.layer.cornerRadius = 20
        leftButton.layer.borderWidth = 1
        leftButton.layer.masksToBounds = true
        leftButton.addTarget(self, action: #selector(ViewController.leftButtonAction(_:)), for: .touchUpInside)

        self.view.addSubview(leftButton)

        rightButton = UIButton(type: .custom)
        rightButton.frame = CGRect(x: 240, y: 300, width: 70, height: 70)
        rightButton.backgroundColor = UIColor.cyan
        rightButton.setTitleColor(UIColor.black, for: .normal)
        rightButton.setTitle("Right button", for: .normal)
        rightButton.layer.cornerRadius = 20
        rightButton.layer.borderWidth = 1
        rightButton.layer.masksToBounds = true
        rightButton.addTarget(self, action: #selector(ViewController.rightButtonAction(_:)), for: .touchUpInside)

        self.view.addSubview(rightButton)

        let margins = self.view.layoutMarginsGuide

        homeNavBar.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
        homeNavBar.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
        homeNavBar.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
        myLabel.topAnchor.constraint(equalTo: homeNavBar.bottomAnchor).isActive = true
        leftButton.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
        leftButton.topAnchor.constraint(equalTo: myLabel.bottomAnchor).isActive = true
        rightButton.topAnchor.constraint(equalTo: myLabel.bottomAnchor).isActive = true
        rightButton.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true

    }

    @objc func leftButtonAction (_ sender: UIButton) {
        let segueSecondViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "idSecond")
        self.show(segueSecondViewController, sender: nil)
        print("I'm activated by the left button.")
    }

    @objc func rightButtonAction (_ sender: UIButton) {
        let segueSecondViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "idSecond")
        self.show(segueSecondViewController, sender: nil)
        print("I'm activated by the right button.")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
import UIKit

class SecondViewController: UIViewController {
    var mySecondLabel: UILabel!
    var mySecondLabelText = "I'm the second page."
    var closeButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        let secondNavBar = UINavigationBar()
        secondNavBar.frame.size = CGSize(width: 320, height: 45)
        secondNavBar.translatesAutoresizingMaskIntoConstraints = false
        let secondNavItem = UINavigationItem(title: "Second page")
        secondNavBar.setItems([secondNavItem], animated: false)

        self.view.addSubview(secondNavBar)

        mySecondLabel = UILabel()
        mySecondLabel.frame = CGRect(x: 165, y: 200, width: 200, height: 42)
        mySecondLabel.backgroundColor = UIColor.yellow
        mySecondLabel.textColor = UIColor.black
        mySecondLabel.text = "\(mySecondLabelText)"
        mySecondLabel.translatesAutoresizingMaskIntoConstraints = false

        self.view.addSubview(mySecondLabel)

        closeButton = UIButton(type: .custom)
        closeButton.frame = CGRect(x: 10, y: 50, width: 50, height: 50)
        closeButton.backgroundColor = UIColor.red
        closeButton.setTitleColor(UIColor.black, for: .normal)
        closeButton.setTitle("Close", for: .normal)
        closeButton.layer.cornerRadius = 30
        closeButton.layer.borderWidth = 1
        closeButton.layer.masksToBounds = true
        closeButton.addTarget(self, action: #selector(SecondViewController.closeButtonAction(_:)), for: .touchUpInside)

        self.view.addSubview(closeButton)

        let margins = self.view.layoutMarginsGuide

        secondNavBar.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
        secondNavBar.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
        secondNavBar.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
        closeButton.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
        closeButton.topAnchor.constraint(equalTo: secondNavBar.bottomAnchor).isActive = true
        mySecondLabel.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
        mySecondLabel.topAnchor.constraint(equalTo: closeButton.bottomAnchor).isActive = true
        mySecondLabel.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true

    }

    @objc func closeButtonAction (_ sender: UIButton) {
        self.dismiss(animated: true, completion: nil)
        print("Second page closed.")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
第二视图控制器:

import UIKit

class ViewController: UIViewController {
    var myLabel: UILabel!
    var leftButton: UIButton!
    var rightButton: UIButton!

// bellow are the 2 variable which I’d like to transport into the second ViewController

    var leftButtonText = "I'm the left button."
    var rightButtonText = "I'm the right button."

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let homeNavBar = UINavigationBar()
        homeNavBar.frame = CGRect(x: 0, y: 0, width: 320, height: 45)
        homeNavBar.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(homeNavBar)

        let homeNavItem = UINavigationItem(title: "Home")
        homeNavBar.setItems([homeNavItem], animated: false)


        myLabel = UILabel()
        myLabel.frame = CGRect(x: 165, y: 150, width: 200, height: 42)
        myLabel.text = "Press one of the button bellow!"

        self.view.addSubview(myLabel)

        leftButton = UIButton(type: .custom)
        leftButton.frame = CGRect(x: 80, y: 300, width: 70, height: 70)
        leftButton.backgroundColor = UIColor.cyan
        leftButton.setTitleColor(UIColor.black, for: .normal)
        leftButton.setTitle("Left Button", for: .normal)
        leftButton.layer.cornerRadius = 20
        leftButton.layer.borderWidth = 1
        leftButton.layer.masksToBounds = true
        leftButton.addTarget(self, action: #selector(ViewController.leftButtonAction(_:)), for: .touchUpInside)

        self.view.addSubview(leftButton)

        rightButton = UIButton(type: .custom)
        rightButton.frame = CGRect(x: 240, y: 300, width: 70, height: 70)
        rightButton.backgroundColor = UIColor.cyan
        rightButton.setTitleColor(UIColor.black, for: .normal)
        rightButton.setTitle("Right button", for: .normal)
        rightButton.layer.cornerRadius = 20
        rightButton.layer.borderWidth = 1
        rightButton.layer.masksToBounds = true
        rightButton.addTarget(self, action: #selector(ViewController.rightButtonAction(_:)), for: .touchUpInside)

        self.view.addSubview(rightButton)

        let margins = self.view.layoutMarginsGuide

        homeNavBar.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
        homeNavBar.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
        homeNavBar.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
        myLabel.topAnchor.constraint(equalTo: homeNavBar.bottomAnchor).isActive = true
        leftButton.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
        leftButton.topAnchor.constraint(equalTo: myLabel.bottomAnchor).isActive = true
        rightButton.topAnchor.constraint(equalTo: myLabel.bottomAnchor).isActive = true
        rightButton.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true

    }

    @objc func leftButtonAction (_ sender: UIButton) {
        let segueSecondViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "idSecond")
        self.show(segueSecondViewController, sender: nil)
        print("I'm activated by the left button.")
    }

    @objc func rightButtonAction (_ sender: UIButton) {
        let segueSecondViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "idSecond")
        self.show(segueSecondViewController, sender: nil)
        print("I'm activated by the right button.")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
import UIKit

class SecondViewController: UIViewController {
    var mySecondLabel: UILabel!
    var mySecondLabelText = "I'm the second page."
    var closeButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        let secondNavBar = UINavigationBar()
        secondNavBar.frame.size = CGSize(width: 320, height: 45)
        secondNavBar.translatesAutoresizingMaskIntoConstraints = false
        let secondNavItem = UINavigationItem(title: "Second page")
        secondNavBar.setItems([secondNavItem], animated: false)

        self.view.addSubview(secondNavBar)

        mySecondLabel = UILabel()
        mySecondLabel.frame = CGRect(x: 165, y: 200, width: 200, height: 42)
        mySecondLabel.backgroundColor = UIColor.yellow
        mySecondLabel.textColor = UIColor.black
        mySecondLabel.text = "\(mySecondLabelText)"
        mySecondLabel.translatesAutoresizingMaskIntoConstraints = false

        self.view.addSubview(mySecondLabel)

        closeButton = UIButton(type: .custom)
        closeButton.frame = CGRect(x: 10, y: 50, width: 50, height: 50)
        closeButton.backgroundColor = UIColor.red
        closeButton.setTitleColor(UIColor.black, for: .normal)
        closeButton.setTitle("Close", for: .normal)
        closeButton.layer.cornerRadius = 30
        closeButton.layer.borderWidth = 1
        closeButton.layer.masksToBounds = true
        closeButton.addTarget(self, action: #selector(SecondViewController.closeButtonAction(_:)), for: .touchUpInside)

        self.view.addSubview(closeButton)

        let margins = self.view.layoutMarginsGuide

        secondNavBar.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
        secondNavBar.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
        secondNavBar.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
        closeButton.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
        closeButton.topAnchor.constraint(equalTo: secondNavBar.bottomAnchor).isActive = true
        mySecondLabel.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
        mySecondLabel.topAnchor.constraint(equalTo: closeButton.bottomAnchor).isActive = true
        mySecondLabel.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true

    }

    @objc func closeButtonAction (_ sender: UIButton) {
        self.dismiss(animated: true, completion: nil)
        print("Second page closed.")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

您可以通过为视图控制器提供一个标识符来实现这一点。这不是一个序列,但它将实例化您的目标VC:

let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewControllerWithIdentifier("identifier") as! SecondViewController

self.navigationController?.pushViewController(vc, animated:true)
您可以在情节提要的Identity Inspector选项卡上设置VC标识符

我刚刚在文档中读到,如果故事板中没有带标识符的segue,则不能调用performsgue方法,因此请编写您的需求,您不能以编程方式执行

标识符标识触发序列的字符串。在里面 在接口生成器中,您可以在 属性检查器


如果两个ViewController都在故事板中,请参见:此方法可以,但我有两个按钮是通过编程创建的。所以,我不能用它。我想使用2个UIStoryboardSegue对象。您可以将其与多个segue一起使用。只要根据需要拖出尽可能多的按钮,并为每个按钮提供自己的标识符。好吧,但如果发生这种情况,我不知道我的ViewController中会有多少个按钮?为什么每个按钮都需要自己的序列?segue真正定义了目标视图控制器及其显示方式。多个按钮可以呈现相同的序列,您可以使用
发送者
来区分行为。每个按钮都会将自己作为发送者发送,并且
prepareFor(segue:sender:)
可以使用按钮的
标记来设置不同的目的地。