ios - 在 UIAlertView 上检测 Yes 和 No

标签 ios objective-c cocoa-touch

在我的 handleLongHeld: 中,

我有以下行

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

我可以检测到何时按下了 okay,正如您所见,第二种方法将其记录到日志中,我可以看到它正在运行。但是,我想不出一种干净的方法来执行上述 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");
    }
}

最佳答案

在您释放按钮后,将触发警报 View 委托(delegate)。试试这个

-  (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");
    }
}

关于ios - 在 UIAlertView 上检测 Yes 和 No,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22495251/

相关文章:

ios - 将视口(viewport)更改为仅显示 ios-charts 折线图的某些间隔

iOS 委托(delegate)与 KVO 的问题

iphone - iOS : won't terminate or handle many exceptions

objective-c - 从 cocoa 中的 xib 加载 View

ios - 确定使用了哪个共享扩展

3d 动画的 ios 问题

ios - 在 UITableView 中使用动态标签宽度管理自动标题高度

ios - 在 drawrect 方法中绘制带动画的矩形?

iphone - stringByAddingPercentEscapesUsingEncoding :? 的反义词是什么?

iphone - 如何让 UIPickerView 向上滑动到另一个 View 上?