【问题标题】:How to make the main thread execution wait until a UIButton Action如何使主线程执行等到 UIButton Action
【发布时间】:2013-10-04 05:35:11
【问题描述】:

当用户按下保存时,我有一个 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 委托方法中编写进一步的步骤。所以请提供一个替代解决方案。

【问题讨论】:

  • 更新并详细说明您的功能您的问题与您迄今为止尝试过的任何代码。
  • 实际上我只是简化了问题,必须保存很多细节。所以我必须做一个简单的代码等待
  • 好的,请慢慢来编辑。
  • 请不要投反对票!
  • 这是学校作业吗?

标签: ios objective-c uibutton uialertview nsthread


【解决方案1】:

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

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

【讨论】:

  • 不幸的是我做不到! ...请阅读问题我无法在委托方法中实现保存逻辑,我需要在保存按钮操作中恢复控制
【解决方案2】:

你试过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");
                      }
                  }];

【讨论】:

    【解决方案3】:

    你想要这个吗?

    - (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 does not always process dispatch_async 中指出的那样,“如果队列正在执行一个块,该队列中的其他块无法启动。”

    【讨论】:

      猜你喜欢
      • 2023-03-04
      • 2021-05-14
      • 1970-01-01
      • 2021-08-10
      • 2016-04-11
      • 1970-01-01
      • 1970-01-01
      • 2014-12-16
      • 2014-07-18
      相关资源
      最近更新 更多