按下按钮时,我正在尝试呼叫并发出警报。我用这个:
-(IBAction)Add {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"add button pressed"
message:@"Add to record"
delegate:nil
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil ];
[alert show];
[alert release];
}
好吧,这里没问题,两个按钮出现,确定并取消。现在我想检测我按下哪个按钮:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0)
{
//just to show its working, i call another alert view
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"OK WORKIng well"
message:@"no error" delegate:nil
cancelButtonTitle:@"IWORKS"
otherButtonTitles:@"NO PRB", nil];
[alert show];
[alert release];
}
else
{
NSLog(@"cancel");
}
}
现在这是问题所在。我无法检测到按下了哪个按钮;第二个alertview没有显示。我已经检查了几次代码,似乎没有任何问题。没有错误/警告。
答案 0 :(得分:32)
要检测按钮单击,警报视图必须具有关联的代理,例如
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"add button pressed"
message:@"Add to record"
delegate:self // <------
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
答案 1 :(得分:14)
这是我使用的代码,也添加了一些我的代码。 **
-(IBAction) Add
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"add button pressed"
message:@"Add to record"
delegate:nil
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
alert.tag=101;//add tag to alert
[alert show];
[alert release];
}
现在当您按下警告按钮时,它将调用clickedButtonAtIndex 但每个警报都应该有一个标识符。所以添加标签,然后
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex**
{
// the user clicked one of the OK/Cancel buttons
if(alertView.tag == 101) // check alert by tag
{
if (buttonIndex == 0)
{
//just to show its working, i call another alert view
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"OK WORKIng well"
message:@"no error"
delegate:nil
cancelButtonTitle:@"IWORKS"
otherButtonTitles:@"NO PRB", nil];
[alert show];
[alert release];
}
else
{
NSLog(@"cancel");
}
}
}
希望它有所帮助。
答案 2 :(得分:7)
buttonIndex为0是取消按钮。我建议使用:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
{
NSLog(@"cancel");
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"OK works" message:@"no error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
答案 3 :(得分:2)
我觉得如果您希望在现有警报视图的按钮点击事件上显示新的警报视图,那么最好使用
- (void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
}
委托方法而不是
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
}
答案 4 :(得分:2)
1)
.h file
@interface MyClassViewController:<UIAlertViewDelegate>
2)
.m file
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Note"
message:@"some message"
delegate:self // must be self to call clickedButtonAtIndex
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
3)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == [alertView cancelButtonIndex]) {
NSLog(@"The cancel button was clicked from alertView");
}
else {
}
}
答案 5 :(得分:1)