0

In my iPhone project I have an unwind segue exiting to PlayView. Inside unwind function as seen below I have a timer which delays for 0.05 seconds and then executes "autoSegue" function which in turn segues to other screens. This works fine on iOS 8 but for iOS7 devices, I am having to increase this delay significantly (up to 1.0 seconds), otherwise, it gives "another segue is already in progress" error.

Is there a way to execute "autoSegue" function automatically when any previous segues have been completed?

@IBAction func unwindToPlay (sender: UIStoryboardSegue){
    var del = NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: "autoSegue", userInfo: nil, repeats: false)
}
Kashif
  • 4,074
  • 6
  • 39
  • 82

1 Answers1

1

Call your autoSegue from viewDiDAppear rather than from your unwind segue. This will only get called after all animation transitions are complete. You won't need a timer. But you may want to set some kind of flag in your unwind segue to trigger the action only after an unwind segue has taken place.

EDIT
As this doesn't seem to be working for you, here is some code...

class RedViewController: UIViewController {
    var segueDidUnwind: Bool = false

    @IBAction func unwindToRed(sender: UIStoryboardSegue) {
        println("unwindToRed");
        segueDidUnwind = true
    }

    override func viewDidAppear(animated: Bool) {
        println("viewDidAppear");
        if (segueDidUnwind) {
            self.showVC()
            segueDidUnwind = false
        }
    }

    func showVC() {
        let vc =  UIViewController()
        self.presentViewController(vc, animated: true, completion: nil)
    }
}

It's an extension of this answer ... to which we can extend the logging...

actionForUnwindButton
prepareForSegue
unwindToRed
viewDidAppear

If viewDidAppear is not getting called in your code, you must have other structure to your project which is not apparent in the question.

Community
  • 1
  • 1
foundry
  • 30,849
  • 8
  • 87
  • 124
  • viewDidAppear does not get called on unwind segue. – Kashif Feb 09 '15 at 20:54
  • @Tpos, I must have misunderstood the structure of your project. I assume that the unwind segue takes you to "PlayView" (UIViewController), which should trigger `viewWillAppear` followed by `viewDidAppear` . – foundry Feb 09 '15 at 21:01
  • Your understanding is correct but since I am not removing PlayView after it is initially launched, it only calls viewWillAppear / viewDidAppear once at its initial launch. Subsequent unwind segues from other views to PlayView do not call viewWillAppear / viewDidAppear in PlayView. Unless I am doing something wrong which is causing that? – Kashif Feb 09 '15 at 21:08
  • @Tpos, seems a bit strange to me. ViewWill / Did should get called in 'normal' circumstances. Are you setting the navigationController delegate? Or using some container view controller setup? – foundry Feb 09 '15 at 21:20
  • My segues are modal type, perhaps thats y viewDidDisappear is not called for PlayView – Kashif Feb 09 '15 at 23:26
  • @TPos, modal type makes no difference. But you should be looking for `viewDidAppear`, not `viewDidDisappear`. – foundry Feb 09 '15 at 23:41
  • @fondry, yes viewDidAppear (typo) – Kashif Feb 09 '15 at 23:42
  • @TPos, see my expanded answer – foundry Feb 10 '15 at 00:27
  • thanks for your detailed input. For some strange reason viewDidAppear was not being called on unwind segue on iOS 8 only. I worked around by checking for iOS version and implemented autoSegue in viewDidAppear for iOS 7 as you suggested and for iOS 8 I left the code as is. – Kashif Feb 10 '15 at 13:04