如何在等待UIAlertview按钮之前等待语句执行

时间:2014-10-28 07:35:52

标签: ios objective-c uialertview

我正在开发聊天应用程序,正在向Skype等朋友发送文件,当我得到文件transfaring请求该方法应该返回BOOL值。所以在显示alertview等待用户响应之后

我的代码是,alertview显示,但UI已被阻止

-(BOOL)incommingFileRequest:(XMPPIQ *)inIq
{
    NSXMLElement *si = [inIq elementForName:@"si"];
    NSXMLElement *file = [si elementForName:@"file"];

    NSDictionary *dic = [file attributesAsDictionary];
    fileId = [inIq attributeStringValueForName:@"id"];
    NSLog(@"---filename--- %@",[file attributeForName:@"name"]);

    NSString *name = [NSString stringWithFormat:@"%@ \n %@ ",[dic objectForKey:@"name"],[ self transformedValue:[dic objectForKey:@"size"]]];
    UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:@"Incommming File" 
                                        message:name 
                                        delegate:self 
                                        cancelButtonTitle:@"Reject" 
                                        otherButtonTitles:@"Accept", nil];

    alrt.tag = 1111; 
    [alrt show];

    while (buttonClicked == -1) {
        [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
    } 

    if (buttonClicked == 1) {
        NSLog(@"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~YES");
        return YES;
    } else {
        NSLog(@"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~NO");
        return NO;
    }
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

    if(alertview.tag == 1111){
       if (buttonIndex == 0){
            buttonClicked = buttonIndex;
       } 
    }
}

1 个答案:

答案 0 :(得分:2)

让ViewController实现UIAlertViewDelegate协议并实现方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // Do whatever you need to do after the alertView closes, depending on button indices

   if(alertview.tag == 1111){

       if (buttonIndex == 0){
        // First Button pressed
       } elif (buttonIndex == 1) {
        // Response if second button pressed
       }

  }
    // etc etc
}