0

The piece of code below prints the content of whichever cell is clicked on in my TableView.

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)  {
    print(self.cell[indexPath.row])
}

I want to use the result that is printed in a label on another ViewController.

How do I get the string value from the function and then use it on on the other view? My thought is to use a global variable but I need to get the string value out first.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
wades
  • 250
  • 2
  • 10
  • Do you want to display another view controller from the `didSelectRowAt` method? – rmaddy Dec 03 '18 at 15:32
  • Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Tamás Sengel Dec 04 '18 at 09:41

2 Answers2

1

For example, You can use simple organization of a singleton of another ViewController (SecondScreen) with var main (in case, as usual, when SecondScreen inited via a Storyboard):

class SecondScreen : UIViewController {
    // 1. add this var
    static var main : SecondScreen? = nil

    // 2. Your some UI element
    @IBOutlet weak var textButton: UIButton!

    // 3. add this method
    func updateUI(string : String) {
        textButton.setTitle(string, for: .normal)
    }

    // 4. setting a var
    override func viewDidLoad() {
        if SecondScreen.main == nil {
            SecondScreen.main = self
        }
    }

    // ... another your and standard methods
}

And you can update your SecondScreen like this:

    let v = SecondScreen.main
    v?.updateUI(string: "yourString")

Also I recommend you to call method async:

DispatchQueue.main.async {
    SecondScreen.main?.updateUI(withString : string)
}

I suggest you to learn more about singletons...

Agisight
  • 1,778
  • 1
  • 14
  • 15
0

At first, when you create a tableView, you have to collect data (string here) of cells in an array or another data collection. And you can get a needed data (strings) with indexPath variable in the method didSelectRowAt. And you can pass the string to another ViewController (let use SecondViewController) with several ways.

Here is an example:

// declaration an array of your strings
var array : [String] = ["First", "Second", "Third", ...]
...
// getting a string from method:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)  {
let string = array[indexPath.row]
print(string)
// next, for example, you need to pass the string to a singleton SecondViewController with static var **main**:
SecondViewController.main?.neededString = string
}

Don't forget update in async DispatchQueue:

DispatchQueue.main.async {
    SecondViewController.main?.updateUI(withString : string)
}
Agisight
  • 1,778
  • 1
  • 14
  • 15
  • Thanks! that is working however I have to go to another page and then back before the second view controller updates. How would I force the second view controller to update in real time – wades Dec 03 '18 at 15:54
  • Try this ways: 1) You have to have an instance of second ViewController in some variable (Second VC can be a controller of subview or superview). And you can call a method of second VC, where you can change it's UI. 2) If you haven't an instance, you can init second VC with your string. – Agisight Dec 03 '18 at 16:13
  • the second view is actually an external monitor, the idea is that when a cell is clicked it displays the value of the cell in a label on the external monitor. I am very new to swift but i basically just need to refresh the external view. is that right? – wades Dec 03 '18 at 16:24
  • You can learn and use a variety of techniques: 1. a singleton, 2. the Notification Center and wait for updates, 3. use a delegates and protocols' methods, 4. use a blocks, 5. use a timer and listening the UserDefaults (crazy, but works). Some ways are described here: [link](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers?rq=1) – Agisight Dec 03 '18 at 16:42
  • I get an error saying SecondScreen has no member 'main' – wades Dec 03 '18 at 16:49
  • Yes, you need to organize a singleton class. – Agisight Dec 03 '18 at 17:07