簡體   English   中英

選擇表格視圖單元格后,將數據傳回上一個視圖控制器

[英]Passing data back to previous view controller after selecting table view cell

在將表格視圖單元格選擇到上一個視圖控制器后,我無法傳遞數據。 在選擇表格視圖單元格后,我幾乎試圖從前一個視圖控制器更改標簽。 任何人都可以幫我解決這個問題嗎? 我正在嘗試在選擇單元格后更改UITextField。

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

您的代表需要設置。 在你的情況下,你必須在prepareForSegue方法中設置它

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

你沒有在你的segue中設置委托,所以你的sendBackFriendList方法中的mDelegate是nil

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM