我有一个类名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];
}
}
答案 0 :(得分:0)
取消按钮将是索引“0”,请尝试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];
}
答案 1 :(得分:0)
在您的子类UIAlertViewDelegate
文件中添加.h
,如下所述..
@interface yourSubClassViewController :UIViewController <UIAlertViewDelegate>{
/// your code
}
@end
答案 2 :(得分:0)
请勿使用取消按钮。使用两个按钮 otherButtonTitles 并使用按钮index == 1和按钮索引== 2。这可能有所帮助。
答案 3 :(得分: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];