2013-07-23 17 views
0

是否有設定的一段代碼使用UIAlertView中之前,檢查是否有一定量的壓力機的一個簡單的方法,集UIAlert出現後IBAction爲的x量按壓

例如,

如果IBAction被按下了30次,然後我們想要顯示一個UIAlertView,我可以做Alert,它只是計算如何計數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{ 

} 

} 

回答

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 
+0

你能否詳細說明或提供一個例子,因爲我有點理解,但不能完全... –

+0

見我更新的答案。 –

+0

我也根據您的代碼編輯了我的問題 –

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