xcode 使用 UIAlertView 检测按钮点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3491724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detecting button click with UIAlertView
提问by Stefan
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.
现在问题来了。我无法检测按下了哪个按钮;第二个警报视图不显示。我已经检查了几次代码,它似乎没有任何问题。也没有错误/警告。
回答by kennytm
To detect the button clicks the alert view must have an associated delegate, e.g.
要检测按钮点击,警报视图必须有一个关联的委托,例如
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"add button pressed"
message:@"Add to record"
delegate:self // <------
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
回答by Rajesh Maurya
This is your code which i used and added some my code also. **
这是我使用的代码并添加了一些我的代码。**
-(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];
}
Now when you press button on alert it will call clickedButtonAtIndex but there should a identifier for every alert. So add tag and then
现在,当您按下警报按钮时,它会调用 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");
}
}
}
Hope it helps.
希望能帮助到你。
回答by Eman yalpsid
The buttonIndex of 0 is the cancel button. I would recommend using:
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];
}
}
回答by Joe M
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
我觉得如果您希望在现有警报视图的按钮单击事件上显示新的警报视图,最好使用
- (void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
}
delegate method instead of
委托方法代替
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
}
回答by Tahir Mehmood
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 {
}
}
回答by Kenneth Anguish
If you prefer your code to be cleaner and no dependency on delegate, you should try the blocks implementation of UIAlertView:
如果你希望你的代码更简洁并且不依赖于委托,你应该尝试 UIAlertView 的块实现:
https://github.com/steipete/PSAlertView
https://github.com/steipete/PSAlertView
Blocks are only supported on iOS 4+ devices though.
不过,块仅在 iOS 4+ 设备上受支持。