0

For example I have two view controller 1. ViewControllerA 2. ViewControllerB

ViewControllerA has a Button named as "Select State" and id is 'buttonState' now I want to change that button title like "Tamil Nadu" in ViewControllerB.

I have tried the code bellow

let vc1 = ViewControllerA()

vc1.buttonState.setTitle("Tamil Nadu", for: .normal)

but it does not works and giving the error is "unexpectedly found nil while unwrapping an Optional value"

Now am looking for your help. Thank you

Uma Madhavi
  • 4,851
  • 5
  • 38
  • 73
  • You can not chnge UI components like this. – dahiya_boy Dec 28 '17 at 07:46
  • what the concept are you tried – Anbu.Karthik Dec 28 '17 at 07:53
  • Doing that you are just creating a new instance of ViewControllerA which won't affect the original one and will not have setup any of the sub views like buttonState. You will need to keep references to original ViewControllerA that ViewControllerB can reference (perhaps in the AppDelegate) or use a more extensive data model. – Upholder Of Truth Dec 28 '17 at 07:57

4 Answers4

0

As per your question I think there could be two cases as I can understand

CASE 1: If you are pushing ViewController A from ViewController B

So it will create a new instance of that ViewController A and in that you can pass any String and set it as a label Title, just declare one string variable and set the value while pushing view controller

Example(Swift) : -

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

viewControllerA.strBtnTitle = "Tamil Nadu"

self.navigationController?.pushViewController(viewControllerA, animated: true)

CASE 2: You are at ViewController B and you want to go back to ViewController A

So in that case you have to use Protocol , Delegate For passback data to previous ViewController and for that you can refer this below link for more info

Passing Data between View Controllers

Let me know if it helps.

Patrick R
  • 6,621
  • 1
  • 24
  • 27
0

You're updating the button's title before the view controller's view has been loaded, so buttonState is nil. Assign the text to a string property and then update your button title once the view has loaded.

class ViewControllerA: UIViewController {

   @IBOutlet private weak var buttonState: UIButton!

   var buttonTitle: String?

    viewDidLoad() {
        super.viewDidLoad() {

            buttonState.setTitle(buttonTitle, for: .normal)
        }
    }

}

let vc1 = ViewControllerA()
vc1.buttonTitle = "Tamil Nadu"
// Now present / push vc1
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
-1

Before you try to set title on button, check that this button is already initialized.

If you want to get instance of buttonState from vc1 like let vc1 = ViewControllerA() you need to create object of buttonState in init() method of vc1. Of course if this buttonState is connected from xib/storyboard, then you need to call:

let vc1 : ViewControllerA = storyboard.instantiateViewController(withIdentifier: "identifierVCA") as! ViewControllerA

then:

if let buttonState = vc1.buttonState {
   buttonState.setTitle("Tamil Nadu", for: .normal)
}
Jasjah
  • 1
-1

When you declare a variable with !; it means you have to be very careful about it. The ? states that the variable can be nil, so in your case, you have to use

vc1.buttonState?.setTitle("Tamil Nadu", for: .normal)

Now, the setTitle will only be executed if vc1.buttonState is not nil.

MrWaqasAhmed
  • 1,479
  • 12
  • 12