Creating a segue programmatically

前端 未结 13 978
名媛妹妹
名媛妹妹 2020-11-22 12:41

I have a common UIViewController that all my UIViewsControllers extend to reuse some common operations.

I want to set up a segue on this \"

13条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 13:27

    Here is the code sample for Creating a segue programmatically:

    class ViewController: UIViewController {
        ...
        // 1. Define the Segue
        private var commonSegue: UIStoryboardSegue!
        ...
        override func viewDidLoad() {
            ...
            // 2. Initialize the Segue
            self.commonSegue = UIStoryboardSegue(identifier: "CommonSegue", source: ..., destination: ...) {
                self.commonSegue.source.showDetailViewController(self.commonSegue.destination, sender: self)
            }
            ...
        }
        ...
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            // 4. Prepare to perform the Segue
            if self.commonSegue == segue {
                ...
            }
            ...
        }
        ...
        func actionFunction() {
            // 3. Perform the Segue
            self.prepare(for: self.commonSegue, sender: self)
            self.commonSegue.perform()
        }
        ...
    }
    

提交回复
热议问题