以编程方式创建segue对象

时间:2018-03-31 18:30:13

标签: swift

我创建了一个包含2个视图控制器的视图应用。在第一个视图控制器中,我有一个标签和2个以编程方式创建的按钮。我从这些按钮到第二个视图控制器有2个segue。我希望每个按钮向第二个视图控制器传送不同的信息。从以编程方式创建这些按钮和segue,我无法将标识符分配给segues。所以,我不能使用performSegue(withIdentifier: sender:)。 我怎么能这样做?

First ViewController:

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


}

第二个ViewController:

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.
    }
    */

}

2 个答案:

答案 0 :(得分:0)

您可以通过为View Controller提供标识符来实现。这不是一个segue,但它会实例化您的目标VC:

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

self.navigationController?.pushViewController(vc, animated:true)

您可以在StoryBoard上的Identity Inspector选项卡上设置VC标识符。

我刚刚在文档中读到,如果在Storyboard中没有带标识符的segue,则无法调用performSegue方法,因此请不要以编程方式执行此操作。

  

identifier标识触发的segue的字符串。在   在Interface Builder中,您可以在中指定segue的标识符字符串   属性检查员。

答案 1 :(得分:-1)

您可以展示视图控制器:

TaskCanceledException

当你想要触发segue时调用这个方法。