5

I'm trying to perform a push segue from a navigation controller, after dismissing a modal that was previously presented over the navigation controller.

Inside modal (UIViewController):

func textFieldShouldReturn(textField: UITextField) -> Bool {
    var searchNav = SearchNavigationController()
    self.dismissViewControllerAnimated(true, completion: {searchNav.goToNextView()})
    return true
}

Inside SearchNavigationController:

func goToNextView() {
    performSegueWithIdentifier("searchNavigationToNextView", sender: self)
}

The issue is that when I hit return, I get this error:

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

The segue is present in my storyboard, going from the UINavigationController to the next view.

enter image description here

EDIT: tried this, where _sender is declared in SingleSearchViewcontroller as var _sender = self and it did not work.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "searchNavigationToSingleSearch" {
            let singleVC = segue.destinationViewController as! SingleSearchViewController

            singleVC._sender = self
        }
    }
mattgabor
  • 2,064
  • 6
  • 25
  • 38
  • You need to call the segue from the appropriate controller that represents its origin. In this case you should probably call the segue on self and not the nav controller – Daniel Galasko Jul 30 '15 at 05:28
  • You can't cal a segue from UINavigationController, you should call it from the top controller of UINavigationController – jinhua liao Jul 30 '15 at 05:31
  • @jinhualiao that makes sense, but I just tried it and got the same error. could it have something to do with the fact that the error has an extra ' in it? Although that is not in my code... – mattgabor Jul 30 '15 at 05:44

2 Answers2

2

Update your code as,

func textFieldShouldReturn(textField: UITextField) -> Bool {
    var searchNav = SearchNavigationController()
    self.dismissViewControllerAnimated(true, completion: {self.presentingViewController.goToNextView()})
    return true
}
Ashish P.
  • 848
  • 5
  • 12
  • I think you are correct, but I'm not sure how to pass the delegate to the modal view controller. I tried something like the code I added above in prepareforsegue but it cannot assign a value of type SearchNavigationController to SingleSearchViewController – mattgabor Jul 30 '15 at 06:03
  • Do I have to do some sort of assignment to indicate this view's presentingViewController? When I try that code it does not let me access self.presentingViewController's methods – mattgabor Jul 30 '15 at 08:13
  • It does not allow me to autocomplete the function name and the error is Cannot invoke 'dismissViewControllerAnimated' with an argument list of type '(Bool, completion: () -> _)' – mattgabor Jul 30 '15 at 08:55
0

I had the same issue that mattgabor had, and i tried Ashish's solution but it didn't work for me.

The problem i had in my code was that inside the completion block of "dismissViewControllerAnimated()", the propery "self.presentingViewController" was already nil when i wanted to call the method from my previous viewController that performed the push (the method "goToNextView()" in this example)

So my solution was just to create a variable to hold the previous viewController before calling "dismissViewControllerAnimated()". Using the current example, it would look like this:

    func textFieldShouldReturn(textField: UITextField) -> Bool {
    if let searchNav = self.presentingViewController as? SearchNavigationController {
        self.dismissViewControllerAnimated(true) {
            searchNav.goToNextView()
        }
    }
    return true
 }
Nahuel Roldan
  • 633
  • 8
  • 13