SWIFT if语句和segue

时间:2016-04-01 03:22:47

标签: swift if-statement

if语句和segue有问题 我可以在他们之间建立联系! 我有一系列这些状态["Florida","NY"]()

我的想法,例如,如果我在表视图上单击"Florida"<(this is "string") 我希望segue将我从这个表视图移动到另一个表视图

如果我在tableView上单击"NewYork",我希望segue将我从此表视图转移到另一个包含NY

信息的表

感谢

3 个答案:

答案 0 :(得分:1)

你可以这样做:

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!
        }
    }
}

然后您的第二个视图控制器会有一个变量,其中存储了您选择的城市,因此您可以相应地设置您的内容

class SelectedCityDetailViewController : UITableViewController {

    // Set during the segue
    var selectedCity : String!

    // The rest of your methods
}

答案 1 :(得分:1)

你可以在didSelectRowAtIndexPath中做这样的事情。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

let currentCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell

if let text = currentCell.textLabel?.text {
if text == "Florida"{
    // segue tableview controller
} else if text == "NY" {
   //another segue
  }
} 
}

答案 2 :(得分:0)

获取viewController并添加一个tableView。添加另外两个视图控制器,一个用于NY信息,另一个包含一个uiTableView。从主控制器创建两个segue到另外两个segue。命名segue,以便您可以在代码中引用它们。在didSelectRowAtIndexPath中添加以下代码:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

let currentCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell

if indexPath.row == 0{

        self.performSegueWithIdentifier("florida", sender: nil)

    }else if indexPath.row == 1{

        self.performSegueWithIdentifier("ny", sender: nil)

    }
}

请检查Link。我已经为你创建了一个演示项目。我认为你是iOS开发的先驱。继续!! :)

相关问题