1、 在头文件中设置委托
@interface CardEdit : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate,UIAlertViewDelegate,UITextFieldDelegate> {
UIPickerView *m_pPickerView;
NSArray *m_data;
NSIntegerchoice;
NSString *AlertName;
NSString *GroupName;
UILabel *llabelGroup;
UITextField *ltextGroup;
}
注意:红色是指定的委托。
2、 实现多个UIAlertView在同一个函数中实现事件触发。
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(AlertName==@"增加名片")
{
if(buttonIndex==0)
{
}
else {
CardInformation *myCard=[[CardInformationalloc]init];
myCard.groupkey=self.choice;
myCard.cardkey=10001;
[self.viewaddSubview:myCard.view];
}
}
if(AlertName==@"修改群组")
{
if(buttonIndex==0)
{
}
else {
GroupName=ltextGroup.text;
NSString *path = [[NSBundlemainBundle]pathForResource:@"MyData"ofType:@"plist"];
NSMutableDictionary *dict = [[NSMutableDictionaryalloc]initWithContentsOfFile:path];
NSArray *temp = [dictallKeys];
NSString *temp2 = [tempobjectAtIndex:choice];
NSMutableDictionary *mydict = [dictobjectForKey:temp2];
[dict setObject:mydictforKey:GroupName];
[dict removeObjectForKey:temp2];
[dict writeToFile:pathatomically:YES];
UIAlertView *alertView=[[UIAlertViewalloc]initWithTitle:@"修改群组成功"message:@"恭喜您,修改群组成功!"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确认",nil];
[alertView show]; //显示弹出框
[alertView setFrame:CGRectMake(alertView.frame.origin.x,alertView.frame.origin.y,alertView.frame.size.width , alertView.frame.size.height)];
AlertName =@"0";
}
}
if(AlertName==@"增加群组")
{
if(buttonIndex==0)
{
}
else {
GroupName=ltextGroup.text;
NSString *path = [[NSBundlemainBundle]pathForResource:@"MyData"ofType:@"plist"];
NSMutableDictionary *dict = [[NSMutableDictionaryalloc]initWithContentsOfFile:path];
NSMutableDictionary *mydict = [[NSMutableDictionaryalloc]init];
[dict setObject:mydictforKey:GroupName];
NSLog(@"%@",GroupName);
[dict writeToFile:pathatomically:YES];
UIAlertView *alertView=[[UIAlertViewalloc]initWithTitle:@"增加群组成功"message:@"恭喜您,增加群组成功!"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确认",nil];
[alertView show]; //显示弹出框
[alertView setFrame:CGRectMake(alertView.frame.origin.x,alertView.frame.origin.y,alertView.frame.size.width , alertView.frame.size.height)];
AlertName =@"0";
}
}
if(AlertName==@"删除群组")
{
if(buttonIndex==0)
{
}
else {
NSString *path = [[NSBundlemainBundle]pathForResource:@"MyData"ofType:@"plist"];
NSMutableDictionary *dict = [[NSMutableDictionaryalloc]initWithContentsOfFile:path];
NSArray *temp = [dictallKeys];
NSString *temp2 = [tempobjectAtIndex:choice];
[dict removeObjectForKey:temp2];
[dict writeToFile:pathatomically:YES];
UIAlertView *alertView=[[UIAlertViewalloc]initWithTitle:@"删除群组成功"message:@"恭喜您,删除群组成功!"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确认",nil];
[alertView show]; //显示弹出框
[alertView setFrame:CGRectMake(alertView.frame.origin.x,alertView.frame.origin.y,alertView.frame.size.width , alertView.frame.size.height)];
AlertName =@"0";
}
}
if(AlertName==@"备份名片")
{
if(buttonIndex==0)
{
}
else {
NSString *path = [[NSBundlemainBundle]pathForResource:@"CardInfo"ofType:@"plist"];
NSMutableDictionary *dict = [[NSMutableDictionaryalloc]initWithContentsOfFile:path];
NSString *path2 = [[NSBundlemainBundle]pathForResource:@"CardInfo2"ofType:@"plist"];
NSString *path3 = [[NSBundlemainBundle]pathForResource:@"MyData"ofType:@"plist"];
NSMutableDictionary *dict2 = [[NSMutableDictionaryalloc]initWithContentsOfFile:path3];
NSString *path4 = [[NSBundlemainBundle]pathForResource:@"MyData2"ofType:@"plist"];
[dict writeToFile:path2atomically:YES];
[dict2 writeToFile:path4atomically:YES];
UIAlertView *alertView=[[UIAlertViewalloc]initWithTitle:@"备份成功"message:@"恭喜您,备份名片成功!"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确认",nil];
[alertView show]; //显示弹出框
[alertView setFrame:CGRectMake(alertView.frame.origin.x,alertView.frame.origin.y,alertView.frame.size.width , alertView.frame.size.height)];
AlertName =@"0";
}
}
if(AlertName==@"还原名片")
{
if(buttonIndex==0)
{
}
else {
NSString *path2 = [[NSBundlemainBundle]pathForResource:@"CardInfo2"ofType:@"plist"];
NSMutableDictionary *dict = [[NSMutableDictionaryalloc]initWithContentsOfFile:path2];
NSString *path = [[NSBundlemainBundle]pathForResource:@"CardInfo"ofType:@"plist"];
[dict writeToFile:pathatomically:YES];
NSString *path4 = [[NSBundlemainBundle]pathForResource:@"MyData2"ofType:@"plist"];
NSMutableDictionary *dict2 = [[NSMutableDictionaryalloc]initWithContentsOfFile:path4];
NSString *path3 = [[NSBundlemainBundle]pathForResource:@"MyData"ofType:@"plist"];
[dict2 writeToFile:path3atomically:YES];
UIAlertView *alertView=[[UIAlertViewalloc]initWithTitle:@"还原成功"message:@"恭喜您,还原名片成功"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确认",nil];
[alertView show]; //显示弹出框
[alertView setFrame:CGRectMake(alertView.frame.origin.x,alertView.frame.origin.y,alertView.frame.size.width , alertView.frame.size.height)];
AlertName = @"0";
}
}
}
3、 在程序中使用UIAlertView,并且实现在UIAlertView上增加各种组件
UIAlertView *alertView=[[UIAlertViewalloc]initWithTitle:@"增加名片"message:nildelegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];
alertView.delegate=self;
[alertView show]; //显示弹出框
[alertView setFrame:CGRectMake(alertView.frame.origin.x,alertView.frame.origin.y,alertView.frame.size.width , alertView.frame.size.height+200)];
for (UIView *Outletin [alertViewsubviews]) { //将按钮放置在弹出框的下面
if(![Outlet isKindOfClass:[UITextFieldclass]]&&![OutletisKindOfClass:[UILabelclass]])
{
UIButton* button = (UIButton*)Outlet;
button.frame =CGRectMake(button.frame.origin.x, button.frame.origin.y+160,button.frame.size.width , button.frame.size.height);
}
}
self.llabelGroup = [[UILabelalloc]initWithFrame:CGRectMake(20,40, 140,20)];
llabelGroup.text =@"选择群组:";
llabelGroup.backgroundColor = [UIColorclearColor];
llabelGroup.textColor = [UIColorwhiteColor];
[alertView addSubview:llabelGroup];
[llabelGrouprelease];
m_pPickerView=[[UIPickerViewalloc]init];
m_pPickerView.showsSelectionIndicator =YES;
m_pPickerView.frame=CGRectMake(20,60, 240,150);
m_pPickerView.delegate=self;
[alertView addSubview:m_pPickerView];
self.AlertName=@"增加名片";