使用 UIAlertView 检测按钮点击
我试图在按下按钮时拨打电话并发出警报。我用这个:
-(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");
}
}
现在这就是问题所在。我无法检测到按下了哪个按钮;第二个警报视图不显示。我已经检查了代码几次,似乎没有任何问题。也没有错误/警告。
I am trying to call and alert when a button is pressed. i use this :
-(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];
}
ok , no problem here, two button came up, OK and cancel. Now i want detect which button is pressed i use:
- (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");
}
}
now here is the problem. i cannnot detect which button is pressed; the 2nd alertview doesnt show. i've check through the code a couple of times, there doesn't seem to be any problem with it. no error/warning too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
要检测按钮点击,警报视图必须有一个关联的委托,例如
To detect the button clicks the alert view must have an associated delegate, e.g.
这是我使用的你的代码,也添加了一些我的代码。
**
现在,当您按下警报按钮时,它将调用 clickedButtonAtIndex
但每个警报都应该有一个标识符。所以添加标签,然后
希望它有帮助。
This is your code which i used and added some my code also.
**
Now when you press button on alert it will call clickedButtonAtIndex
but there should a identifier for every alert. So add tag and then
Hope it helps.
0 的buttonIndex 是取消按钮。我建议使用:
The buttonIndex of 0 is the cancel button. I would recommend using:
我觉得如果您希望在现有警报视图的按钮单击事件上显示新的警报视图,最好使用
委托方法而不是
I feel if you wish to show a new alert view on button click event of an existing alert view, it would be better to use
delegate method instead of
如果您希望代码更简洁并且不依赖于委托,则应该尝试 UIAlertView 的块实现:
https:// github.com/steipete/PSAlertView
不过,块仅在 iOS 4+ 设备上受支持。
If you prefer your code to be cleaner and no dependency on delegate, you should try the blocks implementation of UIAlertView:
https://github.com/steipete/PSAlertView
Blocks are only supported on iOS 4+ devices though.