ios - 一定分数后显示警报 View

标签 ios xcode4 uialertview xcode4.3 alert

我正在 Xcode 上创建一个应用程序,我希望在他们单击按钮 100 次时显示警报 View ,但由于某种原因,当我单击按钮 100 次时没有任何 react ,我会向您显示我在应用程序中拥有的所有代码目前以及我在 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 时,没有任何 react 这是图像

enter image description here

enter image description here
并且没有警报
enter image description here

最佳答案

我不会使用“buttonAction”函数,而是将 if 语句放在您的 plus 函数中。类似这样的东西:

-(IBAction)plus {
    counter=counter + 1;
    count.text = [NSString stringWithFormat:@"%i",counter];
    if(counter == 100)
        [self showAlert];
}

它消除了对那个额外函数的需求,如果无论如何调用它都会增加太多计数器。 (plus 和 buttonAction 都告诉 counter 增加 1,意思是每次 plus 被称为 counter 增加 2)

关于ios - 一定分数后显示警报 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11363769/

相关文章:

xcode4 - 添加不带取消按钮的 UIActionSheet

cocoa-touch - UIAlert 需要几秒钟才能显示

macos - Cocoa OS X 应用程序的 XCode 4 教程

c++ - 从 vector 中获取浮点值

iphone - 在 UIScrollView 中加载 200 多个 subview 图像时程序崩溃

ios - Gamekit 的 GKOctree 找不到元素

iphone - 以不同的方法关闭警报 View

iphone - 如何在我的应用程序中使用 UIKit 本地化字符串

ios - 1080p AVCaptureSession

ios - 如何在 Mac 上查看 Apple Watch 屏幕