0

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
    }
}
Greg
  • 427
  • 3
  • 7
  • 21
  • How are you implementing the `delay` function? Not related to your problem, but is the "returnToDispenseScreenOne" segue an unwind segue? – rdelmar Jun 04 '15 at 22:43
  • Did you define the segue by ctrl-dragging from the bar button item? If so, the segue is triggered automatically when you click the button, before the IBAction. – pbasdf Jun 04 '15 at 22:48
  • @rdelmar The `delay` function was a function I found here on SO by @matt. It works great and is very useful. As to your question, I don't know what an unwind segue is. – Greg Jun 04 '15 at 22:57
  • @pbasdf I did define the segue by cartel-dragging from the bar button item so that would explain my problem. How else can I set up the segue? – Greg Jun 04 '15 at 22:59
  • 1
    If you're not using an unwind, then you're probably doing something wrong. You should not go back to a previous controller with a "normal" segue, because those will not take you back, they will instantiate a new controller. – rdelmar Jun 04 '15 at 22:59
  • 2
    You make the segue by dragging from the view controller icon at the top of your controller, not the button, if you want to invoke it in code. – rdelmar Jun 04 '15 at 23:01
  • @rdelmar Thank you. I had not used that method of creating a segue before. Is that the difference between a "normal" segue and an "unwind" segue? I am not familiar with those terms. If there is a reference where I can read about them, please point me to it. I am using the iBook by Apple "The Swift Programming Language" and i cannot even find the term segue when I search. – Greg Jun 04 '15 at 23:11
  • As per @rdelmar if you want to "go back" to an existing VC, you need to use an "unwind segue" - these need setting up a little differently. See [here](http://stackoverflow.com/q/12561735/3985749) for an explanation. – pbasdf Jun 04 '15 at 23:12
  • If you type "segues" into Xcode's documentation help, you will see several documents you can read. – rdelmar Jun 05 '15 at 00:28

2 Answers2

0

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 } }

Chetan Koli
  • 178
  • 15
  • Thank you for your answer. I will be using the `prepareForSegue` function in my app but not this particular need. – Greg Jun 05 '15 at 19:44
0

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)
}
chrisamanse
  • 4,289
  • 1
  • 25
  • 32
  • Thank you. I really like the delay function that @Matt wrote since it is just a temporary function for me until I start connecting to the server where I will exchange information. The activityIndicator will only be visible while the app is waiting to do the data exchange but I need the delay function during development so I can show my company what they will expect to see. – Greg Jun 05 '15 at 19:43