I’m creating a app on Xcode and i would like a alert view to show when they have click a button 100 times but for some reason when i click the button 100 times nothing happens i will show you all the code i have in the app at the moment and what i have done in the xib and whats connected to what hope you guys can help.
#import <UIKit/UIKit.h>
int counter;
@interface ViewController : UIViewController {
IBOutlet UILabel *count;
}
-(IBAction)plus;
-(IBAction)minus;
-(IBAction)zero;
@end
implementation
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void) buttonAction {
counter++;
if(counter == 100)
[self showAlert];
}
- (void) showAlert {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Tile"
message:@"This is the message"
delegate:nil
cancelButtonTitle:@"Dismiss"
otherButtonTitles:nil];
[alert show];
}
-(IBAction)plus {
counter=counter + 1;
count.text = [NSString stringWithFormat:@"%i",counter];
}
-(IBAction)minus {
counter=counter - 1;
count.text = [NSString stringWithFormat:@"%i",counter];
}
-(IBAction)zero {
counter=0;
count.text = [NSString stringWithFormat:@"%i",counter];
}
- (void)viewDidLoad {
counter=0;
count.text = @"0";
[super viewDidLoad];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
thats all the code i have and the problem I’m getting is the counter works fine its just when i reach 100 nothing happens heres the images


and no alert

Instead of having a “buttonAction” function, I would just put the if statement inside your plus function. Something along the lines of this:
It eliminates the need for that extra function, which would increase counter by too much if called anyway. (both plus and buttonAction tell counter to increase by 1, meaning every time plus is called counter increases by 2)