设置UIAlert在x量的IBAction按下后出现

时间:2013-07-23 23:15:18

标签: objective-c uibutton

在使用UIAlertView之前,是否有一种简单的方法可以设置一段代码来检查一定数量的按键,

例如,

如果IBAction被按下了30次,那么我们想要显示一个UIAlertView,我可以做警报,它只是计算如何计算30个印刷机?

多谢你们两位,我还有一点,这里是代码,我在这里使用count作为int;

它有效,但目的是如果用户购买了升级,则忽略计数......此时,即使用户升级,他们仍然会在每30次按下时获得UIAlert

- (IBAction)setRandomText {

    selectedRecNumber = (arc4random() % kMaxRecords);
    NSString *text = [allText objectAtIndex:selectedRecNumber];
   [randomText setText:text];
    count++;

    if (![MKStoreManager featureBPurchased] ) {

        if(count == 30)
        {
            count = 0;

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Limit" message:@"Reached the Limit!" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"OK",nil];
        [alert show];
        [alert release];


    }else{

    }
}

}

---使用此方法修复EDIT;

   if (kMaxRecords == 35) {

            if(count == 30)
            {
                count = 0;

}else{

}

}

2 个答案:

答案 0 :(得分:1)

您可以轻松自己计算。
只需创建一个int变量,然后在每次用户按下按钮时将其递增 同时检查数字是否大于或等于30,如果是,则重置它并显示UIAlertView。

这是一些伪代码。

@interface YourClass : ParentClass {
   int numberOfPresses = 0;
}
@end

@implementation YourClass
-(IBAction)buttonPressed:(id)sender {
   numberOfPresses += 1;
   if (numberOfPresses >= 30) {
      numberOfPresses = 0;
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Your Alert" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
      [alert show];
   }
}
@end

答案 1 :(得分:0)

最简单的方法是在方法中使用静态变量来计算印刷机数:

-(IBAction)buttonPressed:(id)sender
{
    static NSUInteger presses = 0;

    presses++;

    if(presses == 30)
    {
        presses = 0;

        [[[UIAlertView alloc] initWithTitle:@"" message:@"" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil] show];
    }
}
相关问题