4

I had set UIAlertView in AppDelegate.m file.

But when I choose the button on the alert view.

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

was not working.

I had set UIAlertViewDelegate in the AppDelegate.h file.

and my 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;

            }
        }

But the

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

was not enter this method.

Have anyone know what happened? thanks.

Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
dickfala
  • 3,246
  • 3
  • 31
  • 52

3 Answers3

9

Your code doesn't correctly set the delegate of the alert view.

You need to change the following line so that the delegate is the appropriate object (eg, self):

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil         message:@"my message" delegate:nil cancelButtonTitle:@"ok"         otherButtonTitles:@"set",nil];
sapi
  • 9,944
  • 8
  • 41
  • 71
  • Hi sapi, I had try to set delegate: nil or self, but the method still not working. Do you know what happened? thank you. – dickfala Jun 23 '14 at 04:16
  • @dickfala - Where do you have the delegate method `alertView:clickedButtonAtIndex:`? If it's in the `AppDelegate`, and you set the delegate to `self` in the constructor, the methods should be called. You may want to try `alertView:didDismissWithButtonIndex:` – sapi Jun 23 '14 at 04:59
  • I had try to success, I set delegate to self, and "clean" my project. It's ok! Thank you ,sapi~ ^^ – dickfala Jun 23 '14 at 05:47
2

You have to replace the line

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

By this lines

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

You have to set delegate as self to call the delegate methods.

Ashish Ramani
  • 1,214
  • 1
  • 17
  • 30
1

Check your delegate reference in the .h file. Put on the UIAlertViewDelegate.

@interface YourViewController : UIViewController<UIAlertViewDelegate>
Dhrumil
  • 3,221
  • 6
  • 21
  • 34