从另一个视图中的列表中选择项

时间:2015-11-06 21:21:23

标签: ios swift

我有选择状态按钮。当用户单击它时,他会看到带有状态列表的新表视图。当他选择一个时,他会回到上一个视图(它有一个导航栏控制器)并且按钮名称已更改为选定状态。

我理解我创建一个发送selectedState的segue的原则,但是如何使用我不知道的选定值返回。并有另一个错误

enter image description here

表视图选择两个单元格。这是我的代码:

class StateTableViewController: UITableViewController {
    let selectedState:String = "AZ"
    let states = ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DC", "DE", "FL", "GA",
        "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD",
        "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ",
        "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC",
        "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"]


    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.states.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

        // Configure the cell...
        cell.textLabel?.text = self.states[indexPath.row]

        if (self.states[indexPath.row] == self.selectedState) {
            cell.accessoryType = UITableViewCellAccessoryType.Checkmark
        }

        return cell
    }
}

这个问题类似于How to create selecting option UITableView similar in iOS Settings in Swift?

但我不明白:(

2 个答案:

答案 0 :(得分:1)

这是委托的快速示例,其中MainViewController是包含selectStateButton的第一个viewcontroller,而StatesViewController是在点击显示状态列表的按钮后打开的tableviewcontroller。如果您有任何问题,请随时询问:

class MainViewController: UIViewController, StateSelector {
  @IBOutlet weak var selectStateButton: UIButton!

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "Select state" {
      if let navigationController = segue.destinationViewController as? UINavigationController, let statesViewController = navigationController.topViewController as? StatesViewController {
        statesViewController.delegate = self
        statesViewController.selectedState = (sender as? UIButton)?.titleForState(.Normal)
      }
    }
  }

  func didSelectState(state: String) {
    selectStateButton.setTitle(state, forState: .Normal)
    dismissViewControllerAnimated(true, completion: nil)
  }
}

protocol StateSelector {
  func didSelectState(state: String)
}

class StatesViewController: UITableViewController {
  var delegate: StateSelector?
  var selectedState: String?

  let states = ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DC", "DE", "FL", "GA",
    "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD",
    "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ",
    "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC",
    "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"]

  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return states.count
  }

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("StateCell", forIndexPath: indexPath)

    cell.textLabel?.text = self.states[indexPath.row]

    if (self.states[indexPath.row] == self.selectedState) {
      cell.accessoryType = .Checkmark
    } else {
      cell.accessoryType = .None
    }

    return cell
  }

  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    delegate?.didSelectState(states[indexPath.row])
  }
}

答案 1 :(得分:0)

您可以使用unwindSegue(segue:UISegue)或使用prepareforSegue作为代表提供给目标VC本身。