Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

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)
    }
}
like image 638
Brandon Cao Avatar asked Aug 31 '15 08:08

Brandon Cao


People also ask

How do you pass data backwards in Swift?

To pass data back, the most common approach is to create a delegate property in your detail view controller, like this: class ViewControllerB: UIViewController { var selectedName: String = "Anonymous" weak var delegate: ViewControllerA! }

How do I revert to a previous view controller in Swift?

Open your storyboard where your different viewController are located. Tap the viewController you would like your navigation controller to start from.


1 Answers

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
}
}
like image 178
iRiziya Avatar answered Sep 22 '22 06:09

iRiziya