首先,视图控制器必须得实现协议UIAlertViewDelegate中的方法,并指定delegate为self,才能使弹出的Alert窗口响应点击事件。
具体代码如下:
ViewController.h中的代码如下:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIAlertViewDelegate>
@end
在点击事件中,添加如下代码即可弹出UIAlertView。
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"主意", nil)
message:NSLocalizedString(@"你确定要删除记录吗?", nil)
delegate:self
cancelButtonTitle:NSLocalizedString(@"确定", nil)
otherButtonTitles:NSLocalizedString(@"取消", nil),nil];
[alert show];
[alert release];
确定取消的事件,重写如下方法:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
int tag = buttonIndex;
if (tag == 0) {
[arearecode removeAllObjects];
[timerecode removeAllObjects];
[happenrecode removeAllObjects];
[self showhappenview];
}else{
return ;
}
}
这个控件比较简单。只是贴点代码记录一下而已。