ios - 选择 TableView 单元格后将数据传回先前的 View Controller

标签 ios swift

在选择 TableView 单元格到前一个 View Controller 后,我无法传递数据。在选择一个 TableView 单元格后,我几乎试图从以前的 View Controller 更改标签。谁能帮我解决这个问题?我正在尝试在选择一个单元格后更改 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)
    }
}

表格 View Controller :

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

最佳答案

您的委托(delegate)需要设置。在您的情况下,您必须将其设置在 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
}
}

关于ios - 选择 TableView 单元格后将数据传回先前的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32307458/

相关文章:

ios - 面对 UISwitch 打开/关闭的问题

iphone - 基于单选按钮的 iOS pickerview 值更改

ios - 我应该如何以编程方式更新动态 View 的约束?

ios - 将 UIImagePickerController cameraOverlayView 定位在相机预览的顶部

ios - 将一个 Sprite 保留在屏幕内,但让其他 Sprite 离开

ios - 如何将标签设置为自定义进度条上的给定百分比

Swift/Xcode - 自定义注释 View 中的按钮没有响应

Swift 3 变异函数缺少 ('...inPlace' 方法)

objective-c - SceneKit : how to get distance between two SCNNode ?(ObjC 和 Swift)

ios - 如何修复 sqlite3 libversion 不匹配