UIAlertView buttonIndex不起作用

时间:2013-07-20 13:58:54

标签: objective-c uialertview

我想我错过了一些明显的东西。我有UIAlertView来获取应用评论,但我无法让按钮做任何事情。我在UIAlertViewDelegate中调用了my.h,我还有一个UIAlertview,它只是在IBAction btn上,虽然刚刚取消btn但效果很好

我试过给alert.tag = 1但是从来没有任何区别所以我评论了我的第一个UIAlertview,所以我只有一个警报,仍然没有快乐。我想我错过了一些简单的东西。

我还尝试了alertview.cancelButtonIndexalertview.firstOtherButtonIndex而不是0

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



    if (buttonIndex == 0) {
        NSLog(@"index 0 ");
    }
    else if (buttonIndex == 1) {
        NSLog(@"index 1 ");
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"www.google.com"]];
        NSUserDefaults *rateApp = [NSUserDefaults standardUserDefaults];
        NSInteger appLaunch = [ rateApp integerForKey:@"appLaunch"];
        appLaunch = 0 ;
        [rateApp setInteger: appLaunch forKey:@"appLaunch"];
    }
    else if (buttonIndex == 2) {
        NSLog(@"index 2 ");
    }
}


- (void)viewDidLoad


{
    [super viewDidLoad];

    //rate app  appLaunch == 5 || appLaunch ==10
    NSUserDefaults *rateApp = [NSUserDefaults standardUserDefaults];
    NSInteger appLaunch = [ rateApp integerForKey:@"appLaunch"];
    if (appLaunch == 1  ) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Like this app ?" message:@"Why not rate at the app store" delegate:nil cancelButtonTitle:@"No thanks" otherButtonTitles:@"Yes",@"Remind me later", nil];
      //  alert.tag = 1;
        [alert show];
        }

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

如果要调用其委托方法,AlertView的delegate应该是self而不是nil

使用此代码:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Like this app ?" 
                      message:@"Why not rate at the app store"                                        
                      delegate:self 
                      cancelButtonTitle:@"No thanks" 
                      otherButtonTitles:@"Yes",@"Remind me later", nil];