How to change uibutton name at runtime in c object?

I know this question has been asked many times, and I am also implementing the same foundation for chanding the name uibutton, I think.

Let me first clarify my problem. I have one uibutton named btnType, when I click on one of them to be selected, and after selecting one value, I press the "done" button to hide the collector, and at the same time I change the name of uibutton with the code

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

But with my surprise, it does not change, and the application crashes with the signal EXC_BAD_ACCESS. I do not get where I am making a mistake. I allocated memory for btnType in viewdidLoad. I also use

 -(IBAction)pressAddType { toolBar.hidden = FALSE; dateTypePicker.hidden = FALSE; } 

when you click a button to open the collector. I would also like to mention that I made contact with IB with the TouchUpInside event for pressAddType.

Any guesses? I would 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; 
+4
source share
4 answers

to try

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

when the collector is fired, the button (which was the focus-holding control) will be in the selected state (or highlighted .. check it).

and stop using UIControlEventTouchUpInside in the forState: parameter. this is not a state, this is an event. you pass event id instead of state id

+16
source

The state you pass to setTitle should be something like UIControlStateNormal:

 [b setTitle:@"" forState:UIControlStateNormal]; 
+4
source

Instead

 - (IBAction) pressAddType; 

Decare

 - (IBAction) pressAddType:(id)sender; //or (UIButton *)sender 

and define it as:

 -(IBAction)pressAddType:(id)sender { toolBar.hidden = FALSE; dateTypePicker.hidden = FALSE; [(UIButton *)sender setTitle:btnTitle forState:UIControlEventTouchUpInside]; [(UIButton *)sender setTitleColor:[UIColor redColor] forState:UIControlEventAllEvents]; } 

As you can see, you do not need to have your button as ivar, because it is passed as a parameter to the method when clicked.

+1
source

This answer, why not change the name of the button, is an EXC_BAD_ACCESS error only when you try to access an object not in the stack memory, or this object has a null value. So my advice is - please check your object (btnTitle) in memory or not?

+1
source

Source: https://habr.com/ru/post/1335193/


All Articles