我正在Xcode上创建一个应用程序,我想要一个警报视图,以显示他们点击按钮100次,但由于某种原因,当我点击按钮100次没有任何反应我会告诉你我所有的代码在应用程序中,以及我在xib中做了什么,以及与你们希望帮助的人有什么关系。
#import <UIKit/UIKit.h>
int counter;
@interface ViewController : UIViewController {
IBOutlet UILabel *count;
}
-(IBAction)plus;
-(IBAction)minus;
-(IBAction)zero;
@end
实施
#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
这就是我拥有的所有代码和我得到的问题是计数器工作正常它只是当我达到100没有任何事情发生后图像
没有警报
答案 0 :(得分:0)
您没有调用“buttonAction”方法。试试这个:
-(IBAction)plus {
counter=counter + 1;
[self buttonAction];
count.text = [NSString stringWithFormat:@"%i",counter];
}
-(IBAction)minus {
counter=counter - 1;
[self buttonAction];
count.text = [NSString stringWithFormat:@"%i",counter];
}
答案 1 :(得分:0)
我只是将if语句放在你的plus函数中,而不是使用“buttonAction”函数。有点像这样:
-(IBAction)plus {
counter=counter + 1;
count.text = [NSString stringWithFormat:@"%i",counter];
if(counter == 100)
[self showAlert];
}
它消除了对额外功能的需要,如果被调用则会增加太多计数器。 (plus和buttonAction告诉计数器增加1,意味着每次加上称为计数器增加2)