UIAlertView用法

UIAlertView:通过警告视图让用户选择或者向用户显示文本

1. 最简单的用法,在代码执行到的地方加入如下代码就能实现弹出警告框


UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"

                                                 message:@"这是一个基本的警告框!"

                                                 delegate:nil  

                                                 cancelButtonTitle:@"确定"

                                                 otherButtonTitles:nil];  

[alert show];  

[alert release];

2. 为UIAlertView添加多个按钮


UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"

                                                 message:@"可以选择其中一个按钮:"

                                                 delegate:nil  

                                                 cancelButtonTitle:@"取消"

                                                 otherButtonTitles:@"按钮一", @"按钮二", @"按钮三",@"按钮四",nil];  

[alert show];  

[alert release];

3.实现各个按钮相应方法

要想在本控制器内实现alertview的点击响应方法需要让本控制器成为alertview的代理

@interfaceViewController ()<UIAlertViewDelegate>{


}da



代理中找到这个方法并实现它

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

NSString* msg = [[NSString alloc] initWithFormat:@"您按下的第%d个按钮!",buttonIndex];


UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"提示"

                                                message:msg

                                                delegate:nil

                                                cancelButtonTitle:@"确定"

                                                otherButtonTitles:nil];


[alert show];

[alert release];


[msg release];

}


点击“取消”,“按钮一”,“按钮二”,“按钮三”的索引buttonIndex分别是0,1,2,3,4

4. 手动的取消对话框


[alert dismissWithClickedButtonIndex:0 animated:YES];


5.为UIAlertView添加子视图,子视图的大小需要计算

UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"请等待"

                                                message:nil

                                                delegate:nil  

                                                cancelButtonTitle:nil

                                                otherButtonTitles:nil];  

[alert show];


UIActivityIndicatorView*activeView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

activeView.center = CGPointMake(alert.bounds.size.width/2.0f, alert.bounds.size.height-40.0f);  

[activeView startAnimating];  


[alert addSubview:activeView];  


[activeView release];  

[alert release];  



总结

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

实现UIAlertViewDelegate协议的类必须是继承UIViewController或者UIView,继承NSObject是不行的。

具体代码如下:

ViewController.h中的代码如下:

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

ViewController.m中的详细代码

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


如果是在游戏或其他复杂的应用中,要在主线程里面初始化:

1
2
3
4
5
6
7
8
9
10
11
12
-( void )privateFAlertForm:(NSString *)text
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@ "温馨提示" message:text delegate:self cancelButtonTitle:@ "否" otherButtonTitles:@ "是" , nil];
[alert show];
[alert release];
}
//单例方法在主线程里面实现UIAlertView 的初始化
+ ( void )alertForm:(NSString *)text
{
[[RootViewController sharedInstance] performSelectorOnMainThread:@selector(privateFAlertForm:) withObject:text waitUntilDone:YES];
}


你可能感兴趣的:(uialertview, , ,UIAlertView总结)