Perform push segue after an unwind segue

前端 未结 7 2356
失恋的感觉
失恋的感觉 2020-12-05 02:24

I am working on a camera app where the camera views are shown modally. After I am done with cropping. I perform an unwind segue to the MainPageViewController. (

7条回答
  •  -上瘾入骨i
    2020-12-05 02:53

    A bit late to the party but I found a way to do this without using state flags

    Note: this only works with iOS 9+, as only custom segues support class names prior to iOS9 and you cannot declare an exit segue as a custom segue in storyboards

    1. Subclass UIStoryboardSegue with UIStoryboardSegueWithCompletion

    class UIStoryboardSegueWithCompletion: UIStoryboardSegue {
        var completion: (() -> Void)?
    
        override func perform() {
            super.perform()
            if let completion = completion {
                completion()
            }
        }
    }
    

    2. Set UIStoryBoardSegueWithCompletion as the class for your exit segue

    note: the action for this segue should be unwindToMainMenu to match the original question

    3. Update your unwind @IBAction to execute the code in the completion handler

    @IBAction func unwindToMainMenu(segue: UIStoryboardSegue) {
        if let segue = segue as? UIStoryboardSegueWithCompletion {
            segue.completion = { 
                self.performSegueWithIdentifier("Categories", sender: self) 
            }
        }
    }
    

    Your code will now execute after the exit segue completes its transition

提交回复
热议问题