2011-11-21 4 views
1

두 개의 경고 단추가 있는데 두 번째 단추를 다른 URL로 가져올 수 없으며 첫 번째 단추와 동일한 URL로 이동합니다. 두 번째 경고가 아무런 문제가 없으면 두 번째 경고의 "방문"버튼이 첫 번째 경고와 동일하게 이동합니다.두 번째 AlertView openURL이 작동하지 않습니다.

-(IBAction)creditBtn: (id) sender{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Credits" 
               message:@"Message 
               delegate:self 
               cancelButtonTitle:@"Cancel" 
               otherButtonTitles:@"Visit", nil];  

    [alert show];        
    [alert release]; 
}       

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
     if(buttonIndex==1) { 
     [[UIApplication sharedApplication] openURL: 
         [NSURL URLWithString:@"http://website1.com"]]; 
     } 
} 

-(IBAction)sendBtn: (id) sender2{ 
    UIAlertView *alert2 = [[UIAlertView alloc] 
          initWithTitle:@"Send Me Your Feedback" 
          message:@"Message" 
          delegate:self 
          cancelButtonTitle:@"Cancel" 
          otherButtonTitles:@"Visit", nil]; 
    [alert2 show]; 
    [alert2 release]; 
} 

- (void)alertView2:(UIAlertView *)alertView2 clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    // resign the shake FirstResponder, no keyboard if not 
    //[self resignFirstResponder]; 
    // making the otherButtonTitles button in the alert open a URL 
    if(buttonIndex==0){ 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://website2.com"]]; 
    } 
} 

답변

1

alertView2 대리자 메서드로 문제가 발생했습니다. 델리게이트는 무언가가 발생할 때 자동으로 호출되는 메서드입니다. 이 경우, UIAlertView가 닫힐 때, delagate 메소드 : - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex이 자동으로 호출됩니다. 따라서 귀하의 문제는 귀하의 alert2도 첫 번째 경고와 동일한 위임 방법을 호출합니다.

하지만 정말 쉬운 문제가 있습니다. 이를 수정하기 위해 각 alertView에 태그를 추가합니다. 그런 다음 대리자 메서드에서 태그를 확인하여 해당 경고를 확인합니다. 여기에 그 작업을 수행하는 방법이다 : 나는 변경

//Set up your first alertView like normal: 
-(IBAction)creditBtn: (id) sender{ 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Credits" 
               message:@"Message" 
               delegate:self 
               cancelButtonTitle:@"Cancel" 
               otherButtonTitles:@"Visit", nil]; 
     alert.tag = 0; //Here is that tag 
     [alert show];        
     [alert release]; //Although this is unnecessary if you are using iOS 5 & ARC 
} 

유일한 것은 내가이만큼 각 경고가 다른 태그가, 우리는 그 차이를 알 수 있습니다 의미 경고 0으로 첫 번째 경고를 태그입니다.

는 그런 일을 한 것처럼 두 번째 alertView을 설정 :

-(IBAction)sendBtn:(id)sender{ 
    UIAlertView *alert = [[UIAlertView alloc] 
          initWithTitle:@"Send Me Your Feedback" 
          message:@"Message" 
          delegate:self 
          cancelButtonTitle:@"Cancel" 
          otherButtonTitles:@"Visit", nil]; 
    alert.tag = 1;  //Notice I used a different tag number 
    [alert show]; 
    [alert release]; 
} 

공지는 둘 다 alertViews alert을 지명했다. 나는 이것들을 가변 범위라고 부르기 때문에 그들에게 alert & alert2을 지명 할 필요가 없었습니다. 변수 범위는 변수가 일정 시간 동안 살아 있고 그 다음 죽는 것을 의미합니다. 그래서 귀하의 경우, 그 방법의 끝에 변수 UIAlertView *alert을 만들었 기 때문에, 그 방법의 끝에, 그 변수는 죽습니다. 모두가 거기에있다

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
     if(alert.tag == 0 && buttonIndex == 1)  //Show credits 
       [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://website1.com"]]; 

     else if(alert.tag == 1 && buttonIndex == 1)  //Send feedback 
       [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://website2.com"]]; 
} 

그게 전부 : Article on Variable Scope 그리고 마지막으로, alertView에 응답하는 위임 방법은 폐쇄

: 그에 대한 자세한 정보]의 경우,이 체크 아웃. 더 많은 도움이 필요하면 의견을 보내주십시오.

관련 문제