在UIAlertView上检测是和否

时间:2014-03-19 02:50:23

标签: ios objective-c cocoa-touch

在我的handleLongHeld:

我有以下一行

  if(Users_pressed_ok) // How do I do this?

我可以检测到什么时候按下了,你可以看到第二种方法将它记录到日志中,我可以看到它正常工作。但是,我不能想到一个干净的方法来做上面的if语句。

我能想到的唯一方法是使用为此警报更新的全局变量。

-  (void)handleLongHold:(UILongPressGestureRecognizer*)sender {
    if (sender.state == UIGestureRecognizerStateEnded) {
        NSLog(@"Long press Ended");
    } else if (sender.state == UIGestureRecognizerStateBegan) {
        NSLog(@"Long press detected.");

        NSLog(@"long press ");
        UIImageView *myImage = (UIImageView *)sender.view;
        UIImage *tmpImage = [UIImage imageNamed: @"cancel.png"];

        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle: @"Announcement"
                              message: @"It turns out that you are playing Addicus!"
                              delegate: self
                              cancelButtonTitle:@"Cancel"
                              otherButtonTitles:@"OK",nil];
        [alert show];
        if(Users_pressed_ok) // How do I do this?
        [myImage removeFromSuperview];
    }

}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        NSLog(@"user pressed OK");
    }
    else {
        NSLog(@"user pressed Cancel");
    }
}

4 个答案:

答案 0 :(得分:1)

使用alertView.cancelButtonIndex检查取消按钮和ivar。

答案 1 :(得分:1)

释放按钮后,警报视图委托会被触发。试试这个

-  (void)handleLongHold:(UILongPressGestureRecognizer*)sender {
    if (sender.state == UIGestureRecognizerStateEnded) {
        NSLog(@"Long press Ended");
    } else if (sender.state == UIGestureRecognizerStateBegan) {
        NSLog(@"Long press detected.");

        NSLog(@"long press ");
        UIImageView *myImage = (UIImageView *)sender.view;
        UIImage *tmpImage = [UIImage imageNamed: @"cancel.png"];

        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle: @"Announcement"
                              message: @"It turns out that you are playing Addicus!"
                              delegate: self
                              cancelButtonTitle:@"Cancel"
                              otherButtonTitles:@"OK",nil];
        [alert show];
    }

}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        NSLog(@"user pressed OK");
        [myImage removeFromSuperview];
    }
    else {
        NSLog(@"user pressed Cancel");
    }
}

答案 2 :(得分:0)

如果您不介意使用第三方库,则可以使用UIAlertView的块实现之一。例如https://github.com/jivadevoe/UIAlertView-Blocks

然后你可以在其他地方阻止这样做:

RIButtonItem *cancelItem = [RIButtonItem itemWithLabel:@"Cancel" action:nil];

RIButtonItem *okItem = [RIButtonItem itemWithLabel:@"OK" action:^{

  [myImage removeFromSuperview];

}];


UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle: @"Announcement"
                          message: @"It turns out that you are playing Addicus!"
                          cancelButtonItem:cancelItem
                          otherButtonItems:okItem, nil];
[alert show];

我通常使用cocoapods添加此lib,但您也可以添加为git子模块。

答案 3 :(得分:0)

要回答问题的本质...... UIAlertViewDelegate方法-alertView:clickedButtonAtIndex:会告诉您按下了哪个按钮。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch(buttonIndex) {
        case 0: //first button was selected
            break;
        case 1: //second button was selected
            break;
        //...
        case N: //Nth button was selected
            break;
    }
}

仅供参考:buttonIndex 0将是首先指定的按钮。

因此...

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                message:nil
                                               delegate:self
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"Ok",nil];
//Cancel is 0

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                message:nil
                                               delegate:self
                                      cancelButtonTitle:@"Ok"
                                      otherButtonTitles:nil];
//Ok is 0

UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:nil
                                                 message:nil
                                                delegate:self
                                       cancelButtonTitle:nil
                                       otherButtonTitles:@"Ok",nil];
//Ok is 0

PS:无论如何,这都无关紧要。只有你的逻辑很重要(和代码符合性)