2013-11-15 4 views
0

이 UIAlertview에 함수 호출에 사용하려고하는 태그가 있습니다. fogotpassword 경고보기가 나타나면 재설정 된 alertview가 나타나지만 NSLog (@ "Password")를 호출하려고 할 때; 기능은 reset alertview에서 첫 번째 버튼을 누르면 호출되지 않습니다. 대신 재설정 된 alertview 단추가 다시 나타납니다. 나는 어떤 도움을 주셔서 감사하겠습니다.UIAlertview가 올바른 함수를 호출하지 않습니다.

-(void)viewDidLoad{ 
    forgotPassword = [[UIAlertView alloc] initWithTitle:@"Login Error" 
                  message:@"Your login credentials do not match" 
                  delegate:self 
                cancelButtonTitle:@"Try Again" 
                otherButtonTitles: @"Forgot Password",nil]; 
    [forgotPassword show]; 
} 

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

    forgotPassword.tag = 1; 
    resetPassword.tag = 2; 

    if (buttonIndex == 1 && forgotPassword.tag ==1) 
    { 

     resetPassword = [[UIAlertView alloc] initWithTitle: @"Forgot Password" 
                  message:@"Email"                           delegate:self 
                cancelButtonTitle:@"Cancel" 
                otherButtonTitles:@"Reset Password", nil]; 
     [resetPassword setAlertViewStyle:UIAlertViewStylePlainTextInput]; 

     [resetPassword show]; 

     NSLog(@"RESET"); 

    } 
    if (buttonIndex == 1 && resetPassword.tag == 2) { 
     NSLog(@"Password"); 
    } 
} 

답변

3

당신의 논리가 모두 엉망이 방법으로 태그

시도를 사용할 필요가 없습니다. 두 태그가 항상 true가되도록 설정합니다. 두 가지 경고보기에 대한 ivars가있는 것처럼 보이므로 태그를 제거하고 다음을 수행하십시오.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (alertView == forgotPassword) { 
     if (buttonIndex == alertView.firstOtherButtonIndex) { 
      // show other alert 
     } 
    else if (alertView == resetPassword) { 
     if (buttonIndex == alertView.firstOtherButtonIndex) { 
      // reset 
     } 
    } 
} 
+0

감사합니다. 시간 제한이 다되면 바로 체크 표시가 나타납니다. 난 그냥 또 다른 stackoverflow 대답을 따르고 그것은 최선의 방법을 wasnt 같아요 – user2997441

+0

경고보기 ivars 대신 태그를 사용하여 완벽하게 괜찮아요 명심하십시오. 대리자 메서드가 아닌 경고보기를 만들 때 태그를 설정하기 만하면됩니다. – rmaddy

1

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
if(alertView==forgotPassword) 
    NSLog(@"forgotPassword alert"); 
} 
0

코드가 혼동됩니다. 경고보기 "forgotPassword"를 작성하고 태그를 설정하지 않고 표시하십시오. 그런 다음 alertView : clickedButtonAtIndex : 메소드에서 두 경고보기 모두에 태그를 명시 적으로 설정하면 THEN이 해당 경고보기를 조사하여 태그의 내용을 확인합니다. 이러한 태그는 절대로 변경되지 않습니다.

대신 표시하기 전에 각 경고보기에서 태그를 설정 한 다음 alertView : clickedButtonAtIndex : 메서드에 전달 된 UIAlertView의 태그를 확인해야합니다. 이런 식으로 뭔가 :

-(void)viewDidLoad{ 
    forgotPassword = [[UIAlertView alloc] initWithTitle:@"Login Error" 
                  message:@"Your login credentials do not match" 
                  delegate:self 
                cancelButtonTitle:@"Try Again" 
                otherButtonTitles: @"Forgot Password",nil]; 
    forgotPassword.tag = 1; 
    [forgotPassword show]; 
} 

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

if (buttonIndex == 1 && alertView.tag ==1) 
{ 

    resetPassword = [[UIAlertView alloc] initWithTitle: @"Forgot Password" 
                  message:@"Email"                           delegate:self 
                cancelButtonTitle:@"Cancel" 
                otherButtonTitles:@"Reset Password", nil]; 
    [resetPassword setAlertViewStyle:UIAlertViewStylePlainTextInput]; 
    resetPassword.tag = 2; 
    [resetPassword show]; 

    NSLog(@"RESET"); 

} 
if (buttonIndex == 1 && alertView.tag == 2) { 
    NSLog(@"Password"); 
} 

}

0

당신은 당신의 방법에 alertView.tag == 1과 alertView.tag의 == 2를 사용한다. 지금 당장은 ifgotPassword.tag = 1 및 resetPassword.tag = 2를 명시 적으로 설정했기 때문에 buttonIndex == 1이면 if 문은 항상 true가되며 각 항목을 확인하십시오.

또한 alertView 버튼을 누를 때마다 값을 재설정 할 필요가없는 한 viewDidLoad 내에 태그를 설정해야합니다.

또는 쉬운 방법은 아난드 케이가 말한 것입니다.

-Stephen

0

코드는 중복이며, 어떤 경우에 당신은이 경우에 속성으로 UIAlertView를 사용할 필요가 없습니다되지 감각 .. 있다 .. 사용이 :

-(void)viewDidLoad{ 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Login Error" 
                 message:@"Your login credentials  do not match" 
                 delegate:self 
               cancelButtonTitle:@"Try Again" 
               otherButtonTitles: @"Forgot Password",nil]; 
    alertView.tag = 1; 
    [alertView show]; 
} 

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

    if (buttonIndex == 1) { 
     if(alertView.tag == 1) 
     { 

      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: @"Forgot Password" 
                  message:@"Email"                           delegate:self 
                cancelButtonTitle:@"Cancel" 
                otherButtonTitles:@"Reset Password", nil]; 
      [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput]; 
      alertView.tag = 2; 

      [alertView show]; 

      NSLog(@"RESET"); 

     } 
    } else if (alertView.tag == 2) { 
     NSLog(@"Password"); 
    } 
} 

이 코드는 깨끗하게 사용하고 있습니다.)

+0

왜 던컨의 대답을 복제 했습니까? – rmaddy

+0

나는 그것을 복제하지 않았다. 내 코드는 더 깨끗하다. .. 나는 속성으로 UIAlertView를 사용하지 않는다 ... 게다가 Duncun의 응답에는 2 번 "buttonIndex == 1"이있다. 내 코드가 더 효율적이다. 당신은 48k 포인트를 가지고 있습니다. 어쩌면 당신은 중요한 차이를 쉽게 볼 수 있습니다. –

관련 문제