UIAlertView的实现

转载 2012年03月26日 13:02:29

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 = [dict allKeys];

                            NSString *temp2 = [temp objectAtIndex:choice];

                            NSMutableDictionary *mydict = [dict objectForKey:temp2];

                           

                            [dict setObject:mydict forKey:GroupName];

                            [dict removeObjectForKey:temp2];

                            [dict writeToFile:path atomically: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:mydict forKey:GroupName];

                            NSLog(@"%@",GroupName);

                            [dict writeToFile:path atomically: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 = [dict allKeys];

                            NSString *temp2 = [temp objectAtIndex:choice];

                            [dict removeObjectForKey:temp2];

                            [dict writeToFile:path atomically: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:path2 atomically:YES]; 

                            [dict2 writeToFile:path4 atomically: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:path atomically:YES];

                           

                            NSString *path4 = [[NSBundlemainBundle] pathForResource:@"MyData2"ofType:@"plist"];

                            NSMutableDictionary *dict2 = [[NSMutableDictionaryalloc] initWithContentsOfFile:path4];

                            NSString *path3 = [[NSBundlemainBundle] pathForResource:@"MyData"ofType:@"plist"];

                            [dict2 writeToFile:path3 atomically: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 *Outlet in [alertView subviews]) {    //将按钮放置在弹出框的下面

        if(![Outlet isKindOfClass:[UITextFieldclass]]&&![Outlet isKindOfClass:[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=@"增加名片";


模仿系统方法自定义UIAlertView的实现和代理

背景是博主偷懒依然用了iOS8后被废弃掉的UIAlertView,然后取消的字体比确定要粗,同时为了统一界面风格,需要把UIAlertView统一换成定制的样子,博主就犯难了,又不想大批量更改代码,怎...
  • CodingFire
  • CodingFire
  • 2016年05月27日 17:20
  • 1444

iOS9使用提示框的正确实现方式(UIAlertView is deprecated)

iOS9使用提示框的正确实现方式(UIAlertView is deprecated) 前言 在从iOS8到iOS9的升级过程中,弹出提示框的方式有了很大的改变,在Xcode7 ,iO...
  • ktpd_pro
  • ktpd_pro
  • 2016年12月05日 15:05
  • 1522

UIAlertView基本用法详解和delegate方法调用

1,UIAlertView的基本用法 //调用一个类方法 初始化一个 UIAlertView 对象 当一个按钮上有多个按钮的时候,按钮的索引顺序按照定义按钮时候的顺序排列,第一个是取消,第二...
  • walden00
  • walden00
  • 2015年08月28日 17:11
  • 1449

iOS 实现类似UIAlertView 和UIAlertController弹窗动画效果

UIAlertView和UIAlertController 项目中使用的很频繁的,但是系统的样式很多时候满足不了我们的需求,这时候我们需要自定义AlertView,但是我们又想做到像系统那样的动画效果...
  • qq_30267937
  • qq_30267937
  • 2017年06月01日 17:35
  • 784

自定义UIAlertView样式,实现可替换背景和按钮

原文地址:http://blog.csdn.net/toss156/article/details/7552075  UIAlertView 是一个十分常用的控件,网上也有好多类似的...
  • zq_1230123
  • zq_1230123
  • 2015年03月09日 17:11
  • 383

自定义UIAlertView样式,实现可替换背景和按钮 IOS7无效

原文地址:http://blog.csdn.net/toss156/article/details/7552075】           UIAlertView 是一个十分常用的控件,...
  • l241002209
  • l241002209
  • 2014年01月10日 14:43
  • 1040

【IOS】自定义UIAlertView样式,实现可替换背景和按钮

UIAlertView 是一个十分常用的控件,网上也有好多类似的自定义AlertView的方法。但是感觉效果都不是很好,它们有的是在系统自带的上面添加文本框,也有的是完全自己用UIView来实现,还有...
  • Mirage520
  • Mirage520
  • 2012年09月07日 18:54
  • 581

实现对UIAlertController和UIAlertView判断系统后的简单封装

实现对UIAlertController和UIAlertView判断系统后的简单封装iOS8之后用UIAlertController代替了UIAlertView,所以每次有需要弹窗的时候,都需要先判断...
  • u012717842
  • u012717842
  • 2015年12月17日 13:24
  • 410

【IOS】自定义UIAlertView样式,实现可替换背景和按钮

【原创作品, 欢迎转载,转载请在明显处注明! 谢谢。         原文地址:http://blog.csdn.net/toss156/article/details/7552075】 ...
  • toss156
  • toss156
  • 2012年05月09日 23:23
  • 68481

IOS】自定义UIAlertView样式,实现可替换背景和按钮 此博文包含图片此博文包含视频 (2012-10-24 10:23:25)

原文地址:http://blog.csdn.net/toss156/article/details/7552075】   UIAlertView 是一个十分常用的控件,网上也有...
  • u011032334
  • u011032334
  • 2014年12月31日 16:54
  • 492
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:UIAlertView的实现
举报原因:
原因补充:

(最多只允许输入30个字)