Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a button that goes to another view. I want to perform some code before the segue moves. The problem I am facing is the segue goes to the other page before the code has a chance to finish. So the values that are in the User default don't get changed before the view gets changed. My question is how can I get the code to run and after its done get the segue to fire? Here is what I have so far:

 @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.view = LoginView
}
like image 875
MNM Avatar asked May 27 '16 01:05

MNM


People also ask

How do I segue to navigation controller in Swift?

To create a segue between view controllers in the same storyboard file, Control-click an appropriate element in the first view controller and drag to the target view controller. The starting point of a segue must be a view or object with a defined action, such as a control, bar button item, or gesture recognizer.

What is unwind segue Swift?

An unwind segue lets you jump to any view controller further up your view controller hierarchy, destroying all view controllers preceding the destination. Before actually creating one, let's overview how an unwind segue works. You implement an unwind method in the destination view controller.

What is use of segue in Swift?

Segues are visual connectors between view controllers in your storyboards, shown as lines between the two controllers. They allow you to present one view controller from another, optionally using adaptive presentation so iPads behave one way while iPhones behave another.


1 Answers

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()
   }
}
like image 133
Paulw11 Avatar answered Sep 20 '22 16:09

Paulw11