如何在收到数量的分接头后显示警报视图

时间:2012-10-27 21:57:28

标签: objective-c ios xcode

正如标题暗示我试图弄清楚如何在按钮重复一定数量的点击后显示警报。到目前为止,我想出了

- (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作为操作和插座[{1}}

2 个答案:

答案 0 :(得分: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.
    }
 }

答案 1 :(得分: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];
    }
} 

请记住将0设置为您想要执行此操作的numberTouches,例如在viewDidDissapear中,或者如果用户在其他地方录制。