Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
939 views
in Technique[技术] by (71.8m points)

ios - Perform segues when I want

Evening, I have 2 show segues in my VC. But I want to fires these segues only when my pickerView has a row.count > 0.

This is what I have:

Override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if segue.identifier == "searchImages" {

    if pickerView.numberOfRows(inComponent: 0) > 0  {

        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" {

        if pickerView.numberOfRows(inComponent: 0) > 0  {
            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
            controller.queryString = queryString
        }
    }
}

I know I should use: ShouldperformSomething. But I don't know hot use it. Any tips?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...