-1

我有一个类名 viewController,它在下面有以下代码,它工作正常。但是,当我从我的子类控制器调用 check 时,它不能按我想要的方式工作。UIAlertView显示出来,但它无法检测到按钮索引 0 何时被触摸。任何解决方法。

    -(void)check{
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You Lose!"
                                                message:@"Play Again?"
                                               delegate:self
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:@"OK", nil];
    [alert show];

    }

    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
          if (buttonIndex == 0) {     // and they clicked OK.
                      ViewController*myNewVC = [[ViewController alloc] init];
                      [self presentModalViewController:myNewVC animated:NO];
          }
      }
4

4 回答 4

0

取消按钮将是索引“0”,Try for index==1

使用此方法:

-(void)check{
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You Lose!"
                                                message:@"Play Again?"
                                               delegate:self
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:@"PopUp", nil];
    [alert show];

    }

    - (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
           if (buttonIndex == 0) {     
                     NSLog("Cancel Button clicked);
          }
        else if (buttonIndex == 1)  {
             ViewController*myNewVC = [[ViewController alloc] init];
            [self presentModalViewController:myNewVC animated:NO];
        }
于 2013-07-08T11:29:09.433 回答
0

不要使用取消按钮。使用两个按钮otherButtonTitles并使用按钮 index == 1 和 button index == 2 。这可能会有所帮助。

于 2013-07-08T11:42:01.023 回答
0

您需要将 设置delegate为您的子类。

 -(void)check:(id)delegate{
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You Lose!"
                                            message:@"Play Again?"
                                           delegate:delegate
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:@"OK", nil];
[alert show];

}

你叫它[self check:self];

于 2013-07-08T11:49:55.497 回答
0

添加UIAlertViewDelegate您的子类.h文件,如下所示..

@interface yourSubClassViewController :UIViewController <UIAlertViewDelegate>{
   /// your code

}
@end
于 2013-07-08T11:33:19.083 回答