4

AppDelegate.m ファイルに UIAlertView を設定しました。

しかし、アラートビューのボタンを選択すると。

-(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

このメソッドに入っていませんでした。

何が起こったのか知っている人はいますか?ありがとう。

4

3 に答える 3

9

あなたのコードはアラート ビューのデリゲートを正しく設定していません。

次の行を変更して、デリゲートが適切なオブジェクトになるようにする必要があります (例self: )。

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil         message:@"my message" delegate:nil cancelButtonTitle:@"ok"         otherButtonTitles:@"set",nil];
于 2014-06-23T02:54:32.130 に答える
2

ラインを交換する必要があります

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

この行で

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

デリゲート メソッドを呼び出すには、デリゲートを self として設定する必要があります。

于 2014-06-23T04:42:49.337 に答える
1

.h ファイルでデリゲート参照を確認してください。をつけますUIAlertViewDelegate

@interface YourViewController : UIViewController<UIAlertViewDelegate>
于 2014-06-23T04:25:38.943 に答える