简体   繁体   中英

Passing data from a UITableView to another ViewController depending on what row is selected

I have two ViewControllers one which contains a UITextView and the other one contains a UITableView. I would like my app to pass data for the selected row from the SecondViewController which contains the UITableView to the UITextView in the first ViewController depending on what row the user select. I am using the below code in the firstViewController (Just to give you a bit of history what I have is a UITextView inside the firstViewController and the user have the option of either entering a custom value or exert a longpressgesture then a popover Window get displayed containing the UITableView in the secondViewController. What I would like to achieve is when a row is selected from the popoverView which contains the UItableView the popoverView get closed and the value highlighted in the table get displayed in the UITextView in the firstViewController):

    class ViewController: UIViewController, UITextViewDelegate, UIPopoverPresentationControllerDelegate {

        @IBOutlet weak var indicativeDesignWorkingLifeTextView: UITextView! 

        var textInsideIndicativeDesignWorkingLifeTextView: String? = nil

     override func viewDidLoad() {

            super.viewDidLoad()

            // Do any additional setup after loading the view, typically from a nib.

    indicativeDesignWorkingLifeTextView.text = textInsideIndicativeDesignWorkingLifeTextView

indicativeDesignWorkingLifeTextView.attributedText = placeholderTextInIndicativeDesignWorkingLifeTextView

        }

    }

and the below code in the secondViewController:

@objc func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        func prepare(for segue: UIStoryboardSegue, sender: UITableViewCell?) {

            let toFirstViewController = segue.destination as! ViewController

            // Pass the selected object to the new view controller.

            if let indexPath = self.indicativeDesignWorkingLifeTable.indexPathForSelectedRow {

                let selectedRow = years[indexPath.row]

                toFirstViewController.textInsideIndicativeDesignWorkingLifeTextView = selectedRow

            }

            }

}

However, when I run the simulator and select a row from the table nothing happens inside the UITextView in the firstViewController? All what happens is that the firstViewController gets displayed. Any help is much appreciated.

Thanks, Shadi.

Update your code as:

@objc func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        self.performSegue(withIdentifier: "<set identifier String>", sender: indexPath)

}


func prepare(for segue: UIStoryboardSegue, sender:Any?) {

            let toFirstViewController = segue.destination as! ViewController

            // Pass the selected object to the new view controller.

            if let indexPath = sender as? IndexPath {
                print("indexPath - \(indexPath)")
                let selectedRow = years[indexPath.row]
                 print("selectedRow - \(selectedRow)")
                toFirstViewController.textInsideIndicativeDesignWorkingLifeTextView = selectedRow

            }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM