Jorgen
Jorgen

Reputation: 475

Swift 2.0 Presenting modal view over tableview

I have a tableview where I tap on disclosure button (i) to show another view modally. Every time the modal view appears the app crashes with this error;

NSInvalidArgumentException', reason: 'Application tried to present modally an active controller

This is being called on the ExamTableView;

   override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "showHTML" {
        let vc : HTMLViewController = segue.destinationViewController as! HTMLViewController
        vc.htmlFile = htmlFile
        presentViewController(vc, animated: true, completion: nil)
    }

It's called from the tap of the button in the ExamTableView;

    override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
    let examListing = exams[indexPath.row]
     htmlFile = examListing.htmlFile!
    print("File: " + htmlFile)
    self .performSegueWithIdentifier("showHTML", sender: self)
}

And I do get the new view's viewDidLoad as I get the print of the htmlFile variable.

    override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    print(htmlFile)
}

But as soon as the modal view lands of the screen, the app crashes. I can see it arriving, but then it's gone. Needless to say, I'm new to Swift and of course Swift 2.

Anyone out there that can see what I'm doing wrong?

Upvotes: 0

Views: 837

Answers (1)

MarkHim
MarkHim

Reputation: 5776

You are not supposed to call presentViewController in prepareForSegue, simply remove the line self .performSegueWithIdentifier("showHTML", sender: self) and it will work - assuming you set up the segue correctly in the storyboard

In prepare for segue you should prepare the destination viewcontroller by passing over relevant parameters. PrepareForSegue is called before the transition happens - assuming shouldPerformSegue returns true. presentViewController is already called implicitly after prepareForSegue is finished, triggering it manually will cause the error you stumbled upon.

Hope i could help

Upvotes: 2

Related Questions