SWIFT if 语句和 segue

标签 swift if-statement

我对 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/

相关文章:

swift - 我在 MacBook Pro 上打开终端并出现此错误消息

ios - 为什么我在推送一些 ViewController 后在 tableView 的顶部获得空间?

ios - 解析分析自定义事件未注册

if-statement - 带有 if_match 和 'and' 的 conky

android - 检查EditText的内容是否为settings

swift - 使 UIImage 变成圆形?

ios - 远程通知中的自定义声音 iOS 10,swift 3

C# if 语句速记运算符 (?:) results in unreachable code

python - if else 嵌套的更好方法

java - Java 中的基本 Hangman 游戏(主要涉及字符串操作)