1

当用户按下保存时,我有一个 UITextField 和保存按钮,我想弹出一个警报以确认他是否要保存并等待他的响应。但不幸的是,警报视图显示似乎没有停止执行或等待用户响应。那么我该如何实施这种情况。

- (IBAction)Savebuttonaction:(UIButton *)sender
{
    UIAlertView *view=[[UIAlertView alloc]initWithTitle:@"message" message:@"Do you want to save" delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil];
    [view show];
    if(_isSaveConfirm)
        NSLog(@"Saved");
    else
        NSLog(@"Not Saved");
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0)
    {
        _isSaveConfirm=YES;
    }
    else
    {
        _isSaveConfirm=NO;
    }
}

请注意,我无法在 alertview 委托方法中编写进一步的步骤。所以请提供替代解决方案。

4

3 回答 3

0

您应该像这样处理用户响应。

- (IBAction)Savebuttonaction:(UIButton *)sender
{
    UIAlertView *view=[[UIAlertView alloc]initWithTitle:@"message" message:@"Do you want to save" delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil];
    [view show];

}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0)
    {
        // put your logic to save
        NSLog(@"Saved");
    }
    else
    {
        // if you want handle this response
        // put your logic here, otherwise 
        // don't handle it.
        NSLog(@"Not Saved");
    }
}
于 2013-10-04T05:58:08.460 回答
0

你试过BlocksKit吗?

[UIAlertView showAlertViewWithTitle:@"message"
                            message:@"Do you want to save"
                  cancelButtonTitle:@"Yes" otherButtonTitles:@[@"No"] 
                            handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                      if (buttonIndex == 0) {
                          NSLog(@"Saved");
                      } else {
                          NSLog(@"Not Saved");
                      }
                  }];
于 2013-10-04T06:40:58.953 回答
0

你想要这个吗?

- (void)buttonClicked:(id)sender {
    __block BOOL userClicked = NO;
    NSLog(@"Show alert");
    [UIAlertView showWithTitle:@"Synchronized Alert" message:@"" cancelButtonTitle:@"NO" otherButtonTitles:@[@"YES"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
        NSLog(@"Alert clicked");
        userClicked = YES;
    }];
    while (!userClicked) {
        NSDate* timeout = [NSDate dateWithTimeIntervalSinceNow:1.f];
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:timeout];
    }
    NSLog(@"Do something else");
    // ...
}

上面的代码将记录:

2013-12-09 20:21:07.612 [14728:60b] Show alert
2013-12-09 20:21:14.739 [14728:60b] Alert clicked
2013-12-09 20:21:14.775 [14728:60b] Do something else

如果您想使用普通的 UIAlertView 而不是块样式的 UIAlertView,则userClicked必须将标志初始化为成员变量或全局变量,以便您可以在委托方法中重置它。

@sel(buttonClicked:)注意:如果您在块中调用方法然后将块分配给调度队列,上述代码将不起作用,正如@rob mayoff 在NSRunLoop runMode 中指出的那样并不总是处理 dispatch_async,“如果队列正在执行块,则不会该队列上的其他块可以启动。”

于 2013-12-09T12:47:53.843 回答