简体   繁体   中英

Passing data from embedded TableViewController

I have a TableViewController embedded in a ViewController . My UITableView has toggles. When one of these toggles are toggled I need my number on the ViewController to increase by 1. The issue I am having is not being able to get the text to change. I have supplied a screenshot to help better illustrate.

在此输入图像描述

What I have tried doing is setting a variable var colorViewController: ViewController? in the TableViewController and then trying to set the text from there using colorViewController?.displayNumber.text = screenCount when toggling a toggle. screenCount is the variable that increasing as the toggles are toggled.

How are you setting your colorViewController variable?

You need to set it to the current instance of your ViewController in your ViewController's prepare for segue.

First, you'll need to give the embed segue an identifier. Select the segue in the storyboard and give it an identifier, say ColorPickerTableViewControllerSegue .

Then handle this segue in your ViewController (this is Swift 3 code):

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

    if segue.identifier == "ColorPickerTableViewControllerSegue" {
        if let colorPickerTableViewController = segue.destination as? ColorPickerTableViewController { 
            colorPickerTableViewController.colorViewController = self
        }
    }
}

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