如何解除模态segue然后执行push segue到新视图控制器

时间:2015-07-30 05:16:21

标签: ios swift uiviewcontroller uinavigationcontroller segue

我在解除先前通过push segue呈现的navigation controller后尝试从modal执行navigation controller

内部模态(UIViewController):

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

内部SearchNavigationController

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

问题是,当我点击返回时,我收到此错误:

  

由于未捕获的异常而终止应用   ' NSInvalidArgumentException',原因:'接收者   ()没有segue   标识符' searchNavigationToNextView''

我的storyboard中存在segue,从UINavigationControllernext view

enter image description here

编辑:试过这个,其中_sender在SingleSearchViewcontroller中声明为var _sender = self并且它不起作用。

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

            singleVC._sender = self
        }
    }

2 个答案:

答案 0 :(得分:2)

将您的代码更新为

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

答案 1 :(得分:0)

我遇到了与mattgabor相同的问题,我尝试过Ashish的解决方案,但它对我没有用。

我的代码中遇到的问题是在" dismissViewControllerAnimated()"的完成块内,属于" self.presentingViewController"当我想从我之前执行推送的viewController调用方法时已经是nil(方法" goToNextView()"在这个例子中)

所以我的解决方案就是在调用" dismissViewControllerAnimated()"之前创建一个变量来保存前一个viewController。使用当前示例,它将如下所示:

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