Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

-(IBAction)pressAddType
{
    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. Thanks.

UPDATE:

@interface AddSettingPage : UIViewController<UITextFieldDelegate>
{
    IBOutlet UIButton *btnType;
    NSString *btnTitle;
}
@property (nonatomic, retain) IBOutlet UIButton *btnType;
-(IBAction)pressAddType;//:(id)sender;

Also

@synthesize btnType,btnTitle;
like image 376
Yama Puvar Avatar asked Jan 12 '11 04:01

Yama Puvar


1 Answers

try

[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

like image 111
kdehairy Avatar answered Oct 06 '22 14:10

kdehairy