【问题标题】:Passing data back to previous view controller after selecting table view cell选择表格视图单元格后将数据传回前一个视图控制器
【发布时间】:2015-08-31 08:58:11
【问题描述】:

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

【问题讨论】:

  • @Glorfindel 我检查了,我所拥有的东西不起作用。我在上面发布了我的代码。关于我做错了什么有什么想法吗?

标签: ios swift


【解决方案1】:

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

【讨论】:

  • 我有一个错误说friendListController没有一个名为delegate @iRealMe的成员
  • 它的mDelegate 在你的情况下。
【解决方案2】:

您没有在 segue 中设置委托,因此 mDelegate 在您的 sendBackFriendList 方法中为零

【讨论】:

  • 抱歉,我是 Swift 新手。但是我应该如何解决这个问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多