Perform segues when I want

白昼怎懂夜的黑 提交于 2019-11-29 16:45:39

You should implement shouldPerformSegue instead of checking pickerView.numberOfRows(inComponent: 0) > 0 in your prepareForSegue method.

P.S: Swift 3 Code (I assume that this is what you want).

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
        return pickerView.numberOfRows(inComponent: 0) > 0
}

Now, prepareForSegue method should be similar to:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "searchImages" {
            let controller = (segue.destination) as! WebViewController
            //replacing " " with "+" for google search queries
            let type: WebViewController.SearchType = .image
            let queryString = String(nameLabel.text!.characters.map {
                $0 == " " ? "+" : $0
            })
            controller.searchType = type
            controller.queryString = queryString
            print("2")

        }

        if segue.identifier == "searchWiki" {
            let controller = (segue.destination) as! WebViewController
            //replacing " " with "+" for google search queries
            let type: WebViewController.SearchType = .wiki
            let queryString = String(nameLabel.text!.characters.map {
                $0 == " " ? "+" : $0
            })
            controller.searchType = type
        }
    }

Hope this helped.

You shouldn't be doing the check on the prepare delegate of the UIStoryboardSegue, but on the shouldPerform:

override func shouldPerformSegueWithIdentifier(identifier: String!, sender: AnyObject!) -> Bool {
    return pickerView.numberOfRows(inComponent: 0) > 0
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!