How to Execute Some Code After a Segue Is Done

How to execute some code after a segue is done?

You can use the UINavigationControllerDelegate protocol and then define:

– navigationController:didShowViewController:animated:

Perform some code before a segue after I push a button in swift

You have a couple of options;

The first is to remove the action from the button in IB and then create a segue between the UIViewController object and your next scene;

@IBAction func onLogoutClick(sender: AnyObject) {
//clear all url chache
NSURLCache.sharedURLCache().removeAllCachedResponses()
//null out everything for logout
email = ""
password = ""
self.loginInformation.setObject(self.email, forKey: "email")
self.loginInformation.setObject(self.password, forKey: "password")
self.loginInformation.synchronize()

self.performSegueWithIdentifier("logoutSegue",sender: self)
}

or you can get rid of the @IBAction method and implement prepareForSegueWithIdentifier

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if segue.identifier == "logoutSegue" {
//clear all url chache
NSURLCache.sharedURLCache().removeAllCachedResponses()
//null out everything for logout
email = ""
password = ""
self.loginInformation.setObject(self.email, forKey: "email")
self.loginInformation.setObject(self.password, forKey: "password")
self.loginInformation.synchronize()
}
}

iOS Xcode (swift) - how to execute code after unwind segue

Introducing Bool state like the other answer's suggesting is very bad and must be avoided if possible as it greatly increases the complexity of your app.

Amongst many other patterns, easiest one to solve this kind of problem is by passing delegate object to Controller2.

protocol Controller2Delegate {
func controller2DidReturn()
}

class Controller1: Controller2Delegate {
func controller2DidReturn() {
// your code.
}

func prepareForSegue(...) {
// get controller2 instance

controller2.delegate = self
}
}

class Controller2 {
var delegate: Controller2Delegate!

func done() {
// dismiss viewcontroller

delegate.controller2DidReturn()
}
}

States are evil and is the single biggest source of software bugs.

IOS- How to execute function after segue finishes in objective-c

Post-segue function calls are most easily put in the viewDidLoad() or viewDidAppear() functions of the target view controller subclass.

Perform push segue after an unwind segue

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

Select exit segue from storyboard
Add custom class

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



Related Topics



Leave a reply



Submit