2012-10-27 16 views
0

正如標題所示,我試圖解決在按鈕收到一定數量的水龍頭後如何顯示警報。到目前爲止,香港專業教育學院拿出如何在收到的水龍頭數量後顯示警報視圖

- (IBAction)count:(id)sender { 

{ 
    UITouch *touch = [count]; 
    if (touch.tapCount == 4) 
    { 
} 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"My alert text here" delegate:nil cancelButtonTitle: @"Ok" otherButtonTitles: nil]; 

[alert show]; 

} 

} 

上述心不是工作,我已經建立了我的按鈕count的行動和作爲出口counted

回答

2

代碼不作出了很大的意義(我很驚訝它編譯,是嗎?)。 UITouch不是您選擇按鈕時的一部分。我認爲你需要做的是保持按鈕被按下多少次,並將其存儲爲實例變量。

例如(在您的實現):

@interface ClassName() { 
    NSUInteger m_buttonTouchCount; 
} 
@end 

// Set m_buttonTouchCount to 0 in your init/appear method, whenever it's appropriate to reset it 

- (IBAction)count:(id)sender { 
{ 
    m_touchButtonCount++ 
    if (m_touchButtonCount == 4) 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" 
                 message:@"My alert text here" 
                 delegate:nil 
               cancelButtonTitle:@"Ok" 
               otherButtonTitles:nil]; 
     [alert show]; 
     m_touchButtonCount = 0; // Not sure if you want to reset to 0 here. 
    } 
} 
+0

沒有不編譯只是顯示該ID至少有一個努力來解決它自己。感謝您的幫助,這很好!只需將NSUinteger'm_buttonTouchCount'更改爲'm_touchButtonCount',以供任何其他需要此代碼的人使用。 – JSA986

0

某處定義的代碼的靜態值的開頭:

static int numberTouches; 

與某一值(在viewWillAppear中):

numberTouches = 0;

比:

- (IBAction)count:(id)sender { 
{ 
    numberTouches++; 
    if(numberTouches == 4) 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"My alert text here" delegate:nil cancelButtonTitle: @"Ok" otherButtonTitles: nil]; 
     [alert show]; 
    } 
} 

Remeber到位的viewDidDissapear設爲0您numberTouches要做到這一點,例如,或者如果用戶錄播別的地方。

+0

我個人不會使用'static int numberTouches;',它更像是一個來自C的遺蹟,對於新手來說,在這個上下文中使用靜態是令人困惑的(可能意味着它在全部類的全部實例中是全局的,它只是意味着變量具有文件範圍)。 Obj-C方法更多的是通過使用屬性和類擴展。 – WDUK

+0

@WDUK謝謝你的解釋,你是對的 – edzio27

相關問題