2014-10-28 92 views
-4

你好我正在聊天應用程序,正在發送文件給朋友如Skype,當我有文件傳輸請求該方法應該返回BOOL值。所以顯示alertview後,在等待用戶響應我如何等待語句執行,直到UIAlertview按鈕點擊

我的代碼,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; 
     } 
    } 
} 
+0

有你閱讀任何圍繞'UIAlertView'(https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertView_Class/index.html)或'UIAlertViewDelegate'的Apple文檔? – Popeye 2014-10-28 07:54:58

+0

這可能是因爲它在任何應用程序中都不會起作用。什麼是'buttonClicked'?這宣佈在哪裏?如果你有更多的代碼,那麼你需要分享你有什麼我們不介意讀者 – Popeye 2014-10-28 08:17:00

+0

做好了我的問題/答案的所有downvotes。多麼可悲,把你標示給版主。 – Popeye 2014-10-28 08:55:29

回答

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 
} 
+0

這是簡單的UIAlertview控制器。你是否解決了我的問題。 – 2014-10-28 07:53:03

+0

@NagRaj你明白你的問題嗎? 'clickedButtonAtIndex:'是做你想做的事的方法。如果你想要別的東西,你的問題很不清楚你想要什麼。 – Popeye 2014-10-28 07:55:20

+0

@Popeye無法理解我該如何問這個問題。 – 2014-10-28 07:56:52