UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法

来源:互联网 发布:淘宝客流量来源 编辑:程序博客网 时间:2024/05/20 22:29

UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法

首先,视图控制器必须得实现协议UIAlertViewDelegate中的方法,并指定delegate为self,才能使弹出的Alert窗口响应点击事件。

具体代码如下:

ViewController.h中的代码如下:

[cpp] view plaincopy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController<UIAlertViewDelegate>  
  4.   
  5. @end  


ViewController.m中的详细代码:

[java] view plaincopy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view from its nib  
  5.       
  6.     //初始化AlertView  
  7.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AlertViewTest"  
  8.                                                    message:@"message"  
  9.                                                   delegate:self  
  10.                                          cancelButtonTitle:@"Cancel"  
  11.                                          otherButtonTitles:@"OtherBtn",nil];  
  12.     //设置标题与信息,通常在使用frame初始化AlertView时使用  
  13.     alert.title = @"AlertViewTitle";  
  14.     alert.message = @"AlertViewMessage";  
  15.       
  16.     //这个属性继承自UIView,当一个视图中有多个AlertView时,可以用这个属性来区分  
  17.     alert.tag = 0;  
  18.     //只读属性,看AlertView是否可见  
  19.     NSLog(@"%d",alert.visible);  
  20.     //通过给定标题添加按钮  
  21.     [alert addButtonWithTitle:@"addButton"];  
  22.     //按钮总数  
  23.     NSLog(@"number Of Buttons :%d",alert.numberOfButtons);  
  24.     //获取指定索引的按钮标题  
  25.     NSLog(@"buttonTitleAtIndex1:%@",[alert buttonTitleAtIndex:1]);  
  26.     NSLog(@"buttonTitleAtIndex2:%@",[alert buttonTitleAtIndex:2]);  
  27.     //获取取消按钮的索引  
  28.     NSLog(@"cancelButtonIndex:%d",alert.cancelButtonIndex);  
  29.     //获取第一个其他按钮的索引  
  30.     NSLog(@"firstOtherButtonIndex:%d",alert.firstOtherButtonIndex);  
  31.     //显示AlertView  
  32.     [alert show];  
  33.     [alert release];  
  34. }  
  35.   
  36. #pragma marks -- UIAlertViewDelegate --  
  37. //根据被点击按钮的索引处理点击事件  
  38. -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
  39. {  
  40.     NSLog(@"clickButtonAtIndex:%d",buttonIndex);  
  41. }  
  42.   
  43. //AlertView已经消失时执行的事件  
  44. -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex  
  45. {  
  46.     NSLog(@"didDismissWithButtonIndex");  
  47. }  
  48.   
  49. //ALertView即将消失时的事件  
  50. -(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex  
  51. {  
  52.     NSLog(@"willDismissWithButtonIndex");  
  53. }  
  54.   
  55. //AlertView的取消按钮的事件  
  56. -(void)alertViewCancel:(UIAlertView *)alertView  
  57. {  
  58.     NSLog(@"alertViewCancel");  
  59. }  
  60.   
  61. //AlertView已经显示时的事件  
  62. -(void)didPresentAlertView:(UIAlertView *)alertView  
  63. {  
  64.     NSLog(@"didPresentAlertView");  
  65. }  
  66.   
  67. //AlertView即将显示时  
  68. -(void)willPresentAlertView:(UIAlertView *)alertView  
  69. {  
  70.     NSLog(@"willPresentAlertView");  
  71. }  
  72.   
  73. - (void)viewDidUnload  
  74. {  
  75.     [super viewDidUnload];  
  76.     // Release any retained subviews of the main view.  
  77.     // e.g. self.myOutlet = nil;  


UIAlertView使用全解

http://blog.sina.com.cn/s/blog_5aeb9f7b0101flpj.html

举例:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Default Alert View"message:@"Defalut" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];


标准的双按钮,cancel那个buttonIndex 为0, ok button 的buttonIndex为1


UIAlertView *alertView = [[UIAlertView allocinitWithTitle:@"Default Alert View"message:@"Defalut" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",@“ThirdButton”, nil];

和程序里的顺序一样,cancel   ok   thirdButton 的buttonIndex 分别为0 1 2


UIAlertView *alertView = [[UIAlertView allocinitWithTitle:@"Default Alert View"message:@"Defalut" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",@“ThirdButton”, nil];

同理,cancel   ok   thirdButton FourthButton的buttonIndex 分别为0 1 2 3

[alertView show];


UIAlertView Delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
alertView--->这个不用多说了吧
buttonIndex---->从0开始
可以通过if (buttonIndex == 1) { } 这样的来控制点击了某个按钮需要做什么操作

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
这个方法在动画结束和视图隐藏之后调用

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
这个方法在动画开始和视图隐藏之前调用

- (void)alertViewCancel:(UIAlertView *)alertView
在视图将要被取消之前
例如,用户点击了home键
三个函数的调用顺序依次是:
alertViewCancel----》willDismissWithButtonIndex---》didDismissWithButtonIndex

- (BOOL)alertViewShouldEnableFirstOtherButton(UIAlertView *)alertView
ios 5+
设置yes / no  将会设置alertView 的第一个otherButton的enable属性

- (void)didPresentAlertView:(UIAlertView *)alertView
在视图提交给用户以后调用

-  (void)willPresentAlertView:(UIAlertView *)alertView
在视图提交给用户以前调用

这六个delegate 方法调用的顺序依次是
alertViewShouldEnableFirstOtherButton---->willPresentAlertView--->didPresentAlertView
---->clickedButtonAtIndex---->(如果会触发视图取消,则会调用alertViewCancel)willDismissWithButtonIndex---->didDismissWithButtonIndex

ios4.0以后 alertView不会自动随着程序转向后台而移除
alertView属性
1.alertViewStyle:
UIAlertViewStyleDefault 只弹信息和按钮
UIAlertViewStyleSecureTextInput 有一个textfield加密框
UIAlertViewStylePlainTextInput 有一个不加密的textfield
UIAlertViewStyleLoginAndPasswordInput 有两个textfield,Login和password

只要有textfield就可以用textfieldAtIndex来捕获并进行相应的操作例如换键盘类型

2.cancelButtonIndex
开始是0,如果没有设置cancel button 则是-1

3.delegate
如果没有设置则是nil

4.firstOtherButtonIndex
从0开始,如果没设置则是-1,而且没被设置则会被忽略

5.message 
消息

6.numberOfButtons
只读  alertView中的按钮数量

7.title
标题

8.visible
只读  如果是yes 表示被显示

实例方法
- (NSInteger)addButtonWithTitle:(NSString *)title
返回值是增加的Button的index

- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex
输入buttonIndex 返回button的标题

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
程序自动完成点击buttonIndex的button 并dismiss 整个alertView的操作

- (id)initWithTitle:(NSString *)title message:(NSString)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitles, ...
这个就不用多说了

- (void)show
要显示必须要调用这个alertview才会显示

-  (UITextField *)textfieldAtIndex:(NSInteger)textfieldIndex
返回值是textfield
UIAlertViewStyleDefault 没有
UIAlertViewStyleSecureInput textfieldIndex 只有一个为0
UIAlertViewStylePlainInput textfieldIndex 只有一个为0
UIAlertViewStyleLoginAndPasswordInput textfieldIndex有两个 0 1



0 0