Button changes title every time pressed

和自甴很熟 提交于 2020-01-05 08:55:51

问题


Im trying this, but when i press it it dont changes, it only displays text. Also tried if else functions, but with no results.

.h

IBOutlet UIButton *poga1;
@property (nonatomic, retain)  UIButton *poga1;

.m

@synthesize poga1;

- (void)viewDidLoad
{
[super viewDidLoad];

[poga1 setTitle:@"My Title" forState:UIControlStateNormal];
[poga1 setTitle:@"My Selected Title" forState:UIControlStateSelected];

}

回答1:


Try this: If you added a title through the storyboard (the gui interface) then delete it.

.h //create an IBOutlet action. How? You right-click the button and drag it to the .h file. A pop up happens and you need to click the drop down and switch it to action //should be something like

- (IBAction)yourButton:(id)sender;

.m
//there will be a new function in your .m which is your button action function. 
@synthesize poga1;

- (void)viewDidLoad
{
[super viewDidLoad];

[poga1 setTitle:@"My Title" forState:UIControlStateNormal];
[poga1 setTitle:@"My Selected Title" forState:UIControlStateSelected];

}

- (IBAction)Start:(id)sender
{

/* if you want this to change the text each time it's pressed then, you need a global counter that your increment each time the button is pressed. then you will have a if statement saying if the counter is equal to 1 (pressed once) then set title to hello, if the counter is equal to 7 (pressed 7 times) set title to goodbye. */

 [sender setTitle:@"whatever" forState:UIControlStateNormal];
}



回答2:


You need to capture the action of the button and change the title, cause UIButton doenst have a permanent state as selected, like a switch or a toggle.

If you want to change the title when the user press:

-(IBAction) onClick: (id) sender {
  [(UIButton *) sender setHighlighted:YES];
}


来源:https://stackoverflow.com/questions/9975842/button-changes-title-every-time-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!