user1771823
user1771823

Reputation: 139

Controlling a button text from another view

I have two views and i used two view controllers. From the main view i want to initialize the button in the second view with a certain text.

If I press the button in the second view, then i can store that button reference to a global UIButton, and change the text then onwards.

But the very first time, how can I initialize the button text in the second view from my main view with a particular text ?

Upvotes: 0

Views: 135

Answers (4)

SachinVsSachin
SachinVsSachin

Reputation: 6427

If you make a button in FirstViewController and want to change button properties like title..etc from the second view, then you should make properties in SecondViewController

  @property (nonatomic,assign) UIButton *button;

when you navigate firstViewController to secondViewController just pass the button Object like

obj = [SecondViewController alloc]...]
obj.button = button;//(FirstViewController button object)

Upvotes: 0

NANNAV
NANNAV

Reputation: 4901

you use delegates & @protocol to access other class example,

refer ex, ex1, link

Upvotes: 1

Srikanth
Srikanth

Reputation: 1735

Not sure how you did this. But if you post some code, it will be helpful. If you already have the above code working, then you need to have the following in order for what you are expecting to work.

Say you have ViewControllerA, ViewControllerB as the two view controllers. You can definitely initialize the text in the button when you create the XIB file.

But I am assuming that you want to do this programatically. If you want to access a button in another view controller, you need

a. Access to the other view controller. b. Once you have access to the other view controller, access to the UIButton variable in that view controller.

So you can do the following in the ViewControllerA code,

-(void)changeButtonTextOfVCB{
        viewControllerB.button setTitle:@"MyTitle" forControlState...
 }

and you can have a button in viewControllerA, which will trigger the above method.

Having said this, I am not sure whether you have the whole thing working, where you can even switch between the view controllers

Upvotes: 0

Anton
Anton

Reputation: 2483

You should not do that: every controller should be responsible for its own view. Full stop.

You can always set some poperty on the second controller and use it when the second view appears on the screen to change whatever you like.

Upvotes: 2

Related Questions