0

I am trying to set the title of a button of viewcontroller2 from viewcontroller1, howevever this is the error I am getting:

viewcontroller1
error

viewcontroller2
vc2

How do I fix this?

Community
  • 1
  • 1
J Duh
  • 161
  • 1
  • 11
  • you can't access the button here. run your code without this line **viewController.cinemaButton.setTitle(cinema,Name for: .normal)**. – MRizwan33 Apr 30 '18 at 12:13
  • @MRizwan33 how would i access the button here? – J Duh Apr 30 '18 at 12:14
  • 1
    you have to take a string variable and pass string to it. than in second VC's "viewDidLoad" method setTitle. – MRizwan33 Apr 30 '18 at 12:16
  • You cannot access button in ViewController1. Instead of take one string variable in ViewController2 and access it in ViewController2. In ViewDidLoad() method of Viewcontroller2 set button title. – Manish Mahajan Apr 30 '18 at 12:21

1 Answers1

2

The reason is before initialising class values you can't get them it will be return nil.

Solution:

  1. Make a var in second vc of String type.
  2. pass values from first vc to that String var.
  3. Now set Title of that String var on viewDidLoad Method of second vc.
MRizwan33
  • 2,723
  • 6
  • 31
  • 42