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
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
Selecting a Word in a Uitextview
Disable Wkwebview for Opening Links to Redirect to Apps Installed on My Iphone
How to Set Http Header Fields in Objective-C
How to Pass Data from Child to Parent View Controller? in Swift
iOS 11 Uibarbuttonitem Images Not Sizing
How to Broadcast Multiple Ibeacon Signals from Only One Bluetooth? and How
Limit Formatted Measurement to 2 Digits
Returning Object from Callback in Swift
Leaking Views When Changing Rootviewcontroller Inside Transitionwithview
How to Change the Blue Highlight Color of a Uitableviewcell
Uicontroleventtouchdragexit Triggers When 100 Pixels Away from Uibutton
How to Get Memory Usage in Swift
Exception Type: Exc_Crash (Sigabrt)
How to Disable 4 Finger Gestures on iPad
Determinate Finish Loading Website in Webview with Swift in Xcode
Autolayout Views Make App Crash on Popviewcontroller
Reading Text and Images from a PDF Document in iOS
iOS Swift Navigate to Certain Viewcontroller Programmatically from Push Notification