问题描述:

I have a bar button that I have created an IBAction for that will save a set of data to a server then return the user to the first of 3 data entry screens. In my current code, I am using a delay function to simulate the saving to server since I do not have the project ready to do that yet but I need to show how it will look when done.

Being very new to development in general, I am sure I am just missing a step here but I am at a loss to see what. This code executes exactly as expected until I introduced the performSegueWithIdentifier step. That is to say, when the Save button is tapped, the activity indicator begins animating, the code pauses for 4.0 seconds and then the activity indicator stops animating. When I added the performSegueWithIdentifier step, and the button is tapped, the segue immediately executes.

Your assistance is greatly appreciated.

@IBAction func saveDTrans(sender: UIBarButtonItem) {

// here is where data is saaved to server

appIsWorking ()

delay(4.0){

self.activityIndicator.stopAnimating()

UIApplication.sharedApplication().endIgnoringInteractionEvents()

self.performSegueWithIdentifier("returnToDispenseScreenOne", sender: self) //use this code to perform segue

}

}

网友答案:

If you want to run some code before segue execution then try

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { if (segue.identifier == "Your Segue name") { // Your code that will run before segue execution } }

网友答案:

Instead of delay, you can use an NSTimer:

NSTimer.scheduledTimerWithTimeInterval(4.0, target: self, selector: Selector("goToNextScreen:"), userInfo: nil, repeats: false)

So, in your code:

@IBAction func saveDTrans(sender: UIBarButtonItem) {
// here is where data is saaved to server
    appIsWorking ()

    // Run function goToNextScreen(_:) 4 seconds later
    NSTimer.scheduledTimerWithTimeInterval(4.0, target: self, selector: Selector("goToNextScreen:"), userInfo: nil, repeats: false)
}

func goToNextScreen(sender: AnyObject) {
    self.activityIndicator.stopAnimating()
    UIApplication.sharedApplication().endIgnoringInteractionEvents()
    self.performSegueWithIdentifier("returnToDispenseScreenOne", sender: self)
}
相关阅读:
Top