0
votes

How can I do so the user gets taken to a custom view controller when he/she taps on "okay" button on alert?

I have a main controller -> navigation controller -> second view controller.

Main View Controller has a segue "mainStoryBoard" The alert gets generated on the second view controller.

This is what I have

let alertController = UIAlertController(title: "title", message: "message",     preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { action
    in self.performSegue(withIdentifier: "mainStoryBoard", sender: self)
}))

present(alertController, animated: true, completion: nil)
return

The error I am getting is the following:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver () has no segue with identifier 'mainStoryBoard'

I have set the identifier to 'mainStoryBoard' and still no luck.

Any guidance will be greatly appreciated.

2
What issues are you having with the code you posted?rmaddy
@rmaddy I am getting - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<udidtoolapp.Setup: 0x1014684b0>) has no segue with identifier 'mainStoryBoard''rob
Make sure you have given the segue an identifier EXACTLY equal to "mainStoryBoard"MikeG
Perhaps these search results will help.rmaddy
Are you sure you set it properly? You clicked on the segue and then went to the attributes inspector and filled in the "identifier" field with "mainStoryBoard" (case sensitive)?MikeG

2 Answers

1
votes

Thanks to @MikeG's guidance I was able to get it to work.

The problem was that I created a segue from the main view controller to the second view controller.

Solution

Create a segue from the second view controller to the main view controller and if you have a navigation controller, create the segue from the second view controller to the navigation controller.

The code remained the same.

0
votes

How are you loading your main view controller?. Try to instantiate using instantiateviewcontrollerwithidentifier not with alloc init. Still if you are facing this problem then try to clean the project and run.