2012-04-05 6 views
0

텍스트 입력으로 경고 창을 표시하도록되어이 코드 조각 UIAlertView 표시 할 수 없습니다 :텍스트 필드

self.alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"How are you?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
self.alert.alertViewStyle = UIAlertViewStylePlainTextInput; 
[self.alert show]; 

원인이 오류 :

Thread 7: Program received signal: "EXC_BAD_ACCESS" 

이 자기 방법이다. 경고는 다음과 같이 정의됩니다.

@interface MyClass : NSObject 
{ 
    UIAlertView *alert; 
    id <MyClassDelegate> __unsafe_unretained delegate; 
} 

@property (nonatomic, retain) UIAlertView *alert; 
@property (unsafe_unretained) id <MyClassDelegate> delegate; 
+0

나에게 아무런 문제가 없다. (대부분의 메모리 관리 문제를 해결할 수있는 ARC를 사용하고있다.) UIAlertViewDelegate 구현을 보여줄 수 있습니까? 백 트레이스를 게시 할 수 있습니까? –

+2

이것을 실행하려고하는 iOS의 버전은 무엇입니까 ?? alertViewStyle은 iOS 5 이상에서만 사용할 수 있습니다. iOS 4.3 이하에서이 작업을 시도하면 충돌이 발생합니다. – jmstone617

+0

iOS4에서는 setAlertViewStyle에 대해 "인식 할 수없는 선택기"가 될 수 있습니다. UI 스레드에 있습니까? – SVD

답변

1

문제는 아마도 사용자 정의 때문일 수 있습니다.

왜 그런지 알 수는 없지만 문제는 스레드의 사용 + 경고의 사용자 정의 때문입니다.

주 스레드에서이 경고를 표시하려고 시도 할 수 있습니까? 무슨 일이야?

이 줄에는 오류가 발생할 수 있습니다. self.alert.alertViewStyle = UIAlertViewStylePlainTextInput;

그렇다면 수행해야 할 작업은 기본 스레드에서 수행하는 것입니다.

- (void) yourMethod{ 
    [self performSelectorOnMainThread:@selector(yourMethod2) withObject:nil waitUntilDone:NO]; 
} 

- (void) yourMethod2{ 
    self.alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"How are you?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
    self.alert.alertViewStyle = UIAlertViewStylePlainTextInput; 
    [self.alert show]; 
} 

내가 일을 정확히 모르는 당신에게보다 더 도움이되지만 수 없습니다 죄송하지만, 일을 편집하면 보여줄 때 나는 이미 다른 스레드에서, 문제에 대해 읽어보십시오.

희망이 당신을 도울 것입니다!

+0

나는 너무 피곤하다. 내일 다시 너에게 갈거야. –

0

EXC_BAD_ACCESS은 해제 된 개체에 액세스함으로써 발생합니다. 이 모달의 UIAlertView 종류에 전화를 걸 방지하려면 :

기능 본체 :

-(void)checkSaving 
{ 
    UIAlertView *alert = [[UIAlertView alloc] 
     initWithTitle:@"Do you want to add these results to your database?" 
     message:@"\n\n" 
     delegate:self 
     cancelButtonTitle:@"No" 
     otherButtonTitles:@"Save", nil]; 

    alert.alertViewStyle = UIAlertViewStyleDefault; 
    [alert show]; 

    //this prevent the ARC to clean up : 
    NSRunLoop *rl = [NSRunLoop currentRunLoop]; 
    NSDate *d; 
    d= (NSDate*)[d init]; 
    while ([alert isVisible]) 
    { 
    [rl runUntilDate:d]; 

    } 
} 

당신의 선택 결과 :

@interface yourViewController() 
    -(void)checkSaving 
    - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
//... 
@end 
:

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    // the user clicked one of the OK/Cancel buttons 

    if (buttonIndex == 1)//Save 
    { 
     //do something 

    } 
    if (buttonIndex == 0)//NO 
    { 
     //do something 
    } 
} 

인터페이스 선언의 기능을 등록

전화 문의 :

[self checkSaving]; 

이 정보가 도움이 되었기를 바랍니다.

+0

답변을 편집하고 코드를 읽을 수 있도록 포맷하십시오. – kleopatra