3

I am struggling to pass a selected option in a picker view in one view controller to the label text of a label in another view controller. The VC with the label I want to update has the Storyboard Identifier: "PageContentViewController". The VC with my picker (periodPicker) has a 'Select' button at the bottom of the view that calls the following function when pressed:

@IBAction func selectButtonPressed(_ sender: Any)  {

    let row = periodPicker.selectedRow(inComponent: 0)
    let selected = periodType[row]

    let pageContentViewController = self.storyboard?.instantiateViewController(withIdentifier: "PageContentViewController") as! PageContentViewController

    pageContentViewController.periodLabelTitle = selected

}

The code for the PageContentViewController class is

import UIKit

class PageContentViewController: UIViewController {       

    @IBOutlet weak var periodLabel: UILabel!

    @IBOutlet weak var dateLabel: UILabel!

    var pageIndex: Int = 0
    var periodLabelTitle: String!
    var dateLabelTitle: String!

    override func viewDidLoad() {
        super.viewDidLoad()

        periodLabel.text = periodLabelTitle
        dateLabel.text = dateLabelTitle

    }

}

When navigating to PageContentViewController via the tab buttons (it is a tab-based application), the label text does not update. Where am I going wrong?

I have tried employing the periodLabel.text = periodLabelTitle command in a viewDidAppear method for pageContentViewController but this still didn't work (after the 'Select' button was pressed previously).

I believe it is also relevant to mention that PageContentViewController is a child VC of a UIPageViewController.

b.b.89
  • 111
  • 2
  • 9
  • Possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – klaudas Feb 18 '17 at 15:13
  • Try putting a break point in the `viewDidLoad()` method and check whether the text is empty or not – KrishnaCA Feb 18 '17 at 15:14
  • @KK7 That does not apply. The OP is referring to a tab application which does not use segues. – Shades Feb 18 '17 at 15:16
  • See this answer: http://stackoverflow.com/questions/27307903/swift-tab-bar-view-prepareforsegue – Shades Feb 18 '17 at 15:16

2 Answers2

0

In selectButtonPressed(_:) you are creating a new instance of PageContentViewController:

// Creates a new instance of PageContentViewController and assigns it to pageContentViewController
let pageContentViewController = self.storyboard?.instantiateViewController(withIdentifier: "PageContentViewController") as! PageContentViewController 

To update the label in the viewcontroller that is visible in the tab bar, your have to use that instance and not a new one. Here is an example on how to do this:

tabBarController?.viewControllers[0].periodLabelTitle = selected

Note that I am using index 0 in my example. You may have to change the index if your PageContentViewController is not the first element in your tab bar.

Please also note that while this answeres your question this is probably not the best way to implement something like this because this solution is dependent on the order of the viewcontrollers. I suggest using either

  • Delegation pattern between the viewcontrollers or
  • NSNotificationCenter
  • Key-Value-Observation
naglerrr
  • 2,809
  • 1
  • 12
  • 24
  • Thank you. I have edited the post as I forgot to mention that the target VC where the text label updates is the child of a `UIPageViewController`. Should this approach still work? – b.b.89 Feb 19 '17 at 09:32
  • I have tried putting your suggested code in the `selectButtonPressed` method, but I am getting the error 'Value of type UIViewController has no member 'periodLabelTitle' – b.b.89 Feb 19 '17 at 11:17
0

It seems that you aren't presenting the PageContentViewController from the func selectButtonPressed(_ sender: Any). This means that when you do present PageContentViewController, it is an instance of it where you haven't set the periodLabelTitle

Gyro Technologies
  • 47
  • 1
  • 2
  • 11