我对 if 语句和 segue 有疑问
我可以在它们之间建立联系吗!
我有这些状态的数组 ["Florida","NY"]()
我的想法,例如,如果我点击 "Florida"<(this is "string")
在表格 View 上
我希望 segue 将我从这个 TableView 移动到另一个 TableView
如果我点击 "NewYork"
在 tableView 上,我希望 segue 将我从这个 TableView 移动到另一个具有关于 NY
信息的 TableView
谢谢
最佳答案
你可以这样做:
class SelectCityViewController : UITableViewController {
// store a reference to use in transition
var selectedCity : String?
let cities : [String] = ["New York", "San Francisco", "Los Angeles"]
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = //TODO: your implementation here
cell.textLabel?.text = cities[indexPath.row]
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.selectedCity = cities[indexPath.row]
performSegueWithIdentifier("YOUR_SEGUE_IDENTIFIER", nil)
}
override func tableView(tableView: UITableView, numberOfItemsInSection section: Int) -> Int {
return self.cities.count
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let vc = segue.destinationViewController as? CityDetailViewController {
vc.selectedCity = self.selectedCity!
}
}
}
然后你的第二个 View Controller 会有一个变量,其中存储了你选择的城市,这样你就可以相应地设置你的内容
class SelectedCityDetailViewController : UITableViewController {
// Set during the segue
var selectedCity : String!
// The rest of your methods
}
关于SWIFT if 语句和 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36347460/