简体   繁体   中英

Button changes title every time pressed

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];

}

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];
}

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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM