2013-04-18 4 views
0

UIKit 작업이 mian 스레드에서 수행되어야한다는 것을 알고 있기 때문에 주 경고 스레드에 경고보기가 표시됩니다.주 스레드에서 경고를 보내지 만 화면이 깜박임

-(void)showAlert:(NSString *)alertMessage{ 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertMessage message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [alert show]; 
    }); 

} 

그러나 경고를 해제하면 화면이 깜박입니다. 그래서 그게 내 문제를 해결하지 못 했어, 내가 놓친 게 있니?

답변

0

당신이 쇼 경고 방법은 백그라운드 스레드를 형성 호출하는 경우, 메소드 호출에 대한

[self performSelectorOnMainThread:@selector(showAlert:) withObject:alertMessage waitUntilDone:YES]; 

를 사용하여 시도하십시오 당신이에 UIAlertView를 할당 해달라고 왜

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertMessage message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 
    [alert show]; 
+0

안녕, 나는 이미 내 질문을 게시하기 전에이 방법을 시도 showAlert 방법 내에 도달,하지만 여전히 닫으면 모든 곳에서 깜박이는 화면을지고, 전혀 도움이되지 않는다 경고. – Malloc

+0

자, 그럼 이것이 스레드 때문에 발생 했습니까? 다른 이유로 인해 발생할 수 있습니다. – Ashok

0

에 showAlert 방법을 변경 주 스레드? 또한 솔루션을 사용하면 메모리 누수가 발생할 수 있습니다. 시도해보십시오.

-(void)showAlert:(NSString *)alertMessage{ 


    dispatch_async(dispatch_get_main_queue(), ^{ 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertMessage message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
     [alert show]; 
     [alert release] 
    }); 

} 

또한 "OK"텍스트는 실제로 사례에서 취소 버튼이어야합니다.

+0

안녕하세요. 내 프로젝트가 ARC이므로 내 개체를 출시 할 필요가 없습니다. – Malloc

0

msgDict 내에 적절한 제목과 메시지가있는 UIAlertView를 표시하려는 곳에 다음 코드를 작성하십시오.

NSMutableDictionary *msgDict=[[NSMutableDictionary alloc] init]; 
    [msgDict setValue:@"Title for AlertView" forKey:@"Title"]; 
    [msgDict setValue:@"Message within the AlertView" forKey:@"Message"]; 

    [self performSelectorOnMainThread:@selector(showAlert:) withObject:msgDict 
         waitUntilDone:YES]; 

프로그램 제어는

-(void)showAlert:(NSMutableDictionary *)msgDict 
     { 
      UIAlertView *alert=[[UIAlertView alloc] 
         initWithTitle:[NSString stringWithFormat:@"%@",[msgDict objectForKey:@"Title"]] 
          message:[NSString stringWithFormat:@"%@",[msgDict objectForKey:"Message"]] 
          delegate:nil 
        cancelButtonTitle:nil 
        otherButtonTitles:@"OK", nil]; 
      [alert show]; 
     } 
+0

안녕하세요, 고맙습니다. 왜 NSMutableDictionary를 사용해야하는지 설명해 주시겠습니까? – Malloc

+0

안녕하세요 Malloc, 나는 방법 ShowAlert에 더 많은 값을 전달하기 위해 여기 NSMutableDictionary를 사용했습니다 :. UIAlertView 내에서 여러 OtherButtonTitles를 보내려는 경우 더 유용 할 수 있습니다. 또한 사전을 사용하지 않고 performSelectorOnMainThread를 사용하여 여러 인수를 보낼 수 없습니다. –

관련 문제