Passing data back to previous view controller after selecting table view cell

匿名 (未验证) 提交于 2019-12-03 03:03:02

问题:

I'm having trouble passing data after selecting a table view cell to the previous view controller. I'm pretty much trying to change a label from the previous view controller after selecting a table view cell. Could anyone help me go about this? I'm trying to change the UITextField after selecting a cell.

UIViewController:

class WhoToOdds: UIViewController, sendBack,UITextFieldDelegate{      @IBOutlet var chosenContact: UITextField!       @IBOutlet var oddsTextBox: UITextView!      var friend: String?      override func viewWillAppear(animated: Bool) {         super.viewWillAppear(animated)          navigationController?.setNavigationBarHidden(false, animated: true)     }      func sendNameToPreviousVC(selectedfriendName: String) {         friend = selectedfriendName         chosenContact.text = friend      }       override func viewDidLoad() {         super.viewDidLoad()          // Do any additional setup after loading the view, typically from a nib.      }      override func didReceiveMemoryWarning() {         super.didReceiveMemoryWarning()         // Dispose of any resources that can be recreated.     }      override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {         if segue.identifier == "friendList"{             let friendViewController = (segue.destinationViewController as! friendListController)           var fbRequest = FBSDKGraphRequest(graphPath:"/me/friends", parameters: nil);         fbRequest.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in          if error == nil {              println("Friends are : \(result)")             PFUser.currentUser()?["friend_list"] = result              PFUser.currentUser()?.save()             print(result)              var resultdict = result as! NSDictionary             println("Result Dict: \(resultdict)")             friendViewController.friendArray = resultdict.objectForKey("data") as! NSArray              } }         }     }      @IBAction private func submitChallenge(sender: AnyObject) {         navigationController?.popViewControllerAnimated(true)     } } 

TableViewController:

protocol sendBack {     func sendNameToPreviousVC(contact: String)     }       class friendListController: UITableViewController, UITableViewDataSource, UITableViewDelegate{      var friendArray:NSArray = ["a","b","c"]     var valueDict:NSDictionary = [:]     var mDelegate:sendBack?      var selectedFriend :String?      override func viewWillAppear(animated: Bool) {         super.viewWillAppear(animated)          navigationController?.setNavigationBarHidden(false, animated: true)     }      override func viewDidLoad() {         super.viewDidLoad()     }      override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {         return friendArray.count     }      override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {         let cell = tableView.dequeueReusableCellWithIdentifier("friend", forIndexPath: indexPath) as! UITableViewCell          cell.textLabel!.text = (friendArray[indexPath.row] as! String)          return cell     }      override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){          let indexPath = tableView.indexPathForSelectedRow();         let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!          selectedFriend = currentCell.textLabel!.text as String!          sendBackFriendList(selectedFriend!)         navigationController?.popViewControllerAnimated(true)     }      func sendBackFriendList(name: String){        self.mDelegate?.sendNameToPreviousVC(name)     } } 

回答1:

Your delegate needs to be set. In your case you have to set it inside prepareForSegue method like

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "friendList"{     let friendViewController = (segue.destinationViewController as! friendListController)     friendViewController.mDelegate = self //Include this line //rest of the code } } 


回答2:

You didn't set the delegate in your segue, so mDelegate is nil in your sendBackFriendList method



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