晚上,我的VC中有2个show segues。但是我只想在我的pickerView有一个row.count > 0
时才会触发这些segues。
这就是我所拥有的:
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
}
}
}
我知道我应该使用:ShouldperformSomething
。但我不知道热用它。
有什么提示吗?
答案 0 :(得分:4)
您应该实施shouldPerformSegue,而不是在pickerView.numberOfRows(inComponent: 0) > 0
方法中检查prepareForSegue
。
P.S:Swift 3 Code(我认为这就是你想要的)。
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
return pickerView.numberOfRows(inComponent: 0) > 0
}
现在,prepareForSegue
方法应类似于:
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
}
}
希望这会有所帮助。
答案 1 :(得分:1)
你不应该对UIStoryboardSegue的prepare委托进行检查,而是在shouldPerform上进行检查:
override func shouldPerformSegueWithIdentifier(identifier: String!, sender: AnyObject!) -> Bool {
return pickerView.numberOfRows(inComponent: 0) > 0
}