Oliver Nass
Oliver Nass

Reputation: 59

iOS 8 Xcode 6.1 Swift prepareForSegue Controller which is already presenting (null)

I get the following warning:

Warning: Attempt to present StartViewController: 0x7ff418e1be30 on StartViewController: 0x7ff418c4ab50 which is already presenting (null)

My Code:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // 1
    if segue.identifier == "Add" {

        let alertController: UIAlertController = UIAlertController(title: "Choices", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
        let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
        let button1action: UIAlertAction = UIAlertAction(title: "First Choice", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) -> () in
            self.performSegueWithIdentifier("first", sender: self)
        })
        let button2action: UIAlertAction = UIAlertAction(title: "Second choice", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) -> () in
            self.performSegueWithIdentifier("second", sender: self)
        })
        alertController.addAction(cancelAction)
        alertController.addAction(button1action)
        alertController.addAction(button2action)


      self.presentViewController(alertController, animated: true, completion: nil)

    }

If I press the "Add"-Button in the ViewController, the Alert will popup, and the warn message too. The controller call itself in the last line of the code, but I have no solution to fix it. Can somebody help me?

Upvotes: 0

Views: 440

Answers (1)

pgb
pgb

Reputation: 25001

If you are in prepareForSegue it means you've already triggered a segue, so the presentation of some view controller has already started.

You should not be calling presentViewController from prepareFromSegue but use it to set up the destination view controller instead.

For this case, I think when you Add, instead of triggering a segue you need to trigger an IBAction and move that code there instead.

Upvotes: 1

Related Questions