How to passing Date between two Storyboard with didSelectRowAtIndexPath(in Swift)?

百般思念 提交于 2019-12-08 14:15:26

问题


I need to use didSelectRowAtIndexPath to pass my current Cell after Searching (this is the only way that I found), but how can I pass this cell name to the SecondViewController?

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

   if(tableView ==   self.searchDisplayController!.searchResultsTableView){
    if let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath){
        if let cellTextLabel: UILabel = cell.textLabel{                         
           let  cellSelected = cellTextLabel.text!   

   }
  }
}

In the SecondViewController, I need to use the TextView to present the current text to the selected cell.


回答1:


Try this code :

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

   if(tableView ==   self.searchDisplayController!.searchResultsTableView){
    if let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath){
        if let cellTextLabel: UILabel = cell.textLabel{                         
           let  cellSelected = cellTextLabel.text!  
           // pass your segue name
           self.performSegueWithIdentifier("segueToDetailVC", sender: cellTextLabel.text!)
   }
  }
}

pass data

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "segueToDetailVC")
        {
            var str: String = sender as! String
            // your ViewController class object
            var detail: DetailVC = segue.destinationViewController as! DetailVC
            // pass text in String Variable
            detail.text = str
        }
    }



回答2:


Add an attribute secondViewController in the destination view controller, and use prepareForSegue

   override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
      if (segue.identifier == "segueTest") {
           var svc = segue!.destinationViewController as secondViewController;

           svc.toPass = cellTextLabel.text

       }
    }

in secondViewController you can define one variable like String

var toPass:String!

In the secondViewController under the viewDidLoad function add this code

println(\(toPass))


来源:https://stackoverflow.com/questions/31852554/how-to-passing-date-between-two-storyboard-with-didselectrowatindexpathin-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!