Iphone对话框工具类详解
在项目中我把对话框的弹出与点击事件通过一个工具类给封装起来了,重要的是要在调用这个工具类的时候实现工具类中的协议,这个协议的方法是为了告诉页面到底点击了哪种对话筐的哪一个button,然后页面中可以根据传过去的不同值做出不同的事件响应,代码如下:
.h文件
[cpp]
#import <Foundation/Foundation.h>
@protocol myAlertviewutilDelegate <NSObject>
-(void)myalertviewbuttonclik:(int)num;
@end
@inte易做图ce AlertViewUtil : NSObject<UIAlertViewDelegate> {
//自定义的对话筐协议
id<myAlertviewutilDelegate> utildelegate;
UIAlertView *myonealertview;//一个按钮的对话筐
UIAlertView *mytwoalertview;//两个按钮的对话筐
UIAlertView *mythreealertview;//三个按钮的对话筐
}
@property(nonatomic,retain) id<myAlertviewutilDelegate> utildelegate;
-(void)alertviewutilone:(NSString *)buttontitleone addmessage:(NSString *)message;
-(void)alertviewutiltwo:(NSString *)buttontitleone twobutton:(NSString *)buttontitletwo addmessage:(NSString *)message;
-(void)alertviewutilthree:(NSString *)buttontitleone twobutton:(NSString *)buttontitletwo threebutton:(NSString *)buttontitlethree addmessage:(NSString *)message;
@end
.m文件
[cpp]
#import "AlertViewUtil.h"
@implementation AlertViewUtil
@synthesize utildelegate;
-(void)dealloc{
[utildelegate release];
[super dealloc];
}
//实现系统对话筐协议中的button点击方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//点击的是第一个alertview
if (alertView == myonealertview) {
if (buttonIndex == 0) {
[self.utildelegate myalertviewbuttonclik:11];
}
[myonealertview release];
}
//点击的是第二个alertview
else if (alertView == mytwoalertview) {
if (buttonIndex == 0) {
[self.utildelegate myalertviewbuttonclik:21];
}
else if(buttonIndex == 1){
[self.utildelegate myalertviewbuttonclik:22];
}
[mytwoalertview release];
}
//点击的是第三个alertview
else if (alertView == mythreealertview) {
if (buttonIndex == 0) {
[self.utildelegate myalertviewbuttonclik:31];
}
else if(buttonIndex == 1){
[self.utildelegate myalertviewbuttonclik:32];
}
else if(buttonIndex == 2){
[self.utildelegate myalertviewbuttonclik:33];
}
[mythreealertview release];
}
}
//只有一个按钮 的alertview
-(void)alertviewutilone:(NSString *)buttontitleone addmessage:(NSString *)message{
myonealertview = [[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:nil otherButtonTitles:buttontitleone, nil];
[myonealertview show];
}
//两个按钮 的alertview
-(void)alertviewutiltwo:(NSString *)buttontitleone twobutton:(NSString *)buttontitletwo addmessage:(NSString *)message{
mytwoalertview = [[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:nil otherButtonTitles:buttontitleone,buttontitletwo, nil];
[mytwoalertview show];
}
//三个按钮 的alertview
-(void)alertviewutilthree:(NSString *)buttontitleone twobutton:(NSString *)buttontitletwo threebutton:(NSString *)buttontitlethree addmessage:(NSString *)message{
mythreealertview = [[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:nil otherButtonTitles:buttontitleone,buttontitletwo,buttontitlethree, nil];
[mythreealertview show];
}
@end
摘自 RiverAM的专栏
补充:移动开发 , IOS ,