4

AppDelegate.m 파일에 UIAlertView를 설정했습니다.alertView : (UIAlertView *) alertView 메서드가 Appdelegate에서 작동하지 않습니다.

그러나 경고보기에서 단추를 선택할 때.

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

이 작동하지 않았습니다.

AppDelegate.h 파일에 UIAlertViewDelegate를 설정했습니다.

내 AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:  (NSDictionary *)launchOptions 
     { 

     [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(reachabilityChanged:) 
               name:kReachabilityChangedNotification 
               object:nil]; 

      NSString *remoteHostName = REACHABILITY_HOST; 
      _hostReachability = [Reachability reachabilityWithHostName:remoteHostName]; 
      [_hostReachability startNotifier]; 

      return YES; 
     } 

     - (void) reachabilityChanged:(NSNotification *)note 
     { 
     if([Reachability isExistNetwork] == NotReachable) 
      { 
       UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil   message:@"my message" delegate:nil cancelButtonTitle:@"ok"   otherButtonTitles:@"set",nil]; 
       [alertView show]; 

      } 

     } 

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

      switch (buttonIndex) { 

       case 0: 
        NSLog(@"ok!"); 
        break; 

        // set 
       case 1: 

        NSLog(@"set!"); 
        NSURL*url=[NSURL URLWithString:@"prefs:root=WIFI"]; 
        [[UIApplication sharedApplication] openURL:url]; 
        break; 

      } 
     } 

그러나

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

이 방법을 입력되지 않았습니다.

아무도 알아 냈습니까? 감사.

+0

appDelegate 클래스는 를 준수해야합니다. 예 : @interface AppDelegate : NSObject . –

답변

9

코드가 경고보기의 대리인을 올바르게 설정하지 않습니다.

당신은 대리인이 적절한 객체 (예를 들어, self)이되도록 다음 줄을 변경해야

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil   message:@"my message" delegate:nil cancelButtonTitle:@"ok"   otherButtonTitles:@"set",nil]; 
+0

안녕하세요 sapi, 대리인을 설정하려고했습니다 : nil 또는 self,하지만 여전히 작동하지 않는 메서드입니다. 무슨 일이 있었는지 아십니까? 고맙습니다. – dickfala

+0

@dickfala - 당신은 대리자 메서드'alertView : clickedButtonAtIndex :'를 가지고 있습니까? 'AppDelegate'에 있고 생성자에서 델리게이트를'self'로 설정하면 메소드가 호출되어야합니다. 당신은'alertView : didDismissWithButtonIndex :' – sapi

+0

을 시도해 볼 수 있습니다. 성공을 위해 노력했고, 저는 자신에게 위임자를 설정하고 프로젝트를 "정리"했습니다. 괜찮아! 고마워, 사피 ~ ^^ – dickfala

1

.H 파일의 대리자 참조를 확인하십시오. UIAlertViewDelegate을 입력하십시오.

@interface YourViewController : UIViewController<UIAlertViewDelegate> 
2

당신은 당신은 위임 메소드를 호출 자체로 대리자를 설정해야이 라인

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"my message" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:@"set",nil]; 
[alertView show]; 

하여 선

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"my message" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:@"set",nil]; 
[alertView show]; 

를 교체해야합니다.