Sarah Sarah - 1 year ago 170
Objective-C Question

how to change uibutton title at runtime in objective c?

I know this question is asked many a times,and i am also implementing the same funda for chanding the title of the uibutton i guess.

Let me clarify my problem first. I have one uibutton named btnType, on clicking of what one picker pops up and after selecting one value,i am hitting done button to hide the picker and at the same time i am changing the the title of the uibutton with code

[btnType setTitle:btnTitle forState:UIControlEventTouchUpInside];
[btnType setTitleColor:[UIColor redColor] forState:UIControlEventAllEvents];

But with my surpriaze,it is not changed and application crashes with signal EXC_BAD_ACCESS. I am not getting where i am making mistake.I have allocated memory to the btnType at viewdidLoad. Also I am using

toolBar.hidden = FALSE;
dateTypePicker.hidden = FALSE;

event on pressing the button to open the picker. Also i would like to mention that i have made connection with IB with event TouchUpInside for pressAddType.

Any guesses? I will be grateful if you could help me.


@interface AddSettingPage : UIViewController<UITextFieldDelegate>
IBOutlet UIButton *btnType;
NSString *btnTitle;
@property (nonatomic, retain) IBOutlet UIButton *btnType;


@synthesize btnType,btnTitle;

Answer Source


[yourButton setTitle:@"your title" forState:UIControlStateNormal];
[yourButton setTitle:@"your title" forState:UIControlStateSelected];
[yourButton setTitle:@"your title" forState:UIControlStateHighlighted];

when the picker is dismissed, the button (which was the control that hold focus) will be in the selected state (or the highlighted .. check it out).

and stop using UIControlEventTouchUpInside in the forState: parameter. it is not a state, it is an event. you are passing an event identifier instead of a state identifier

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download