一、基础用法
[[self window] makeKeyAndVisible];
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"不好啦!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alter show];
[alter release];
return YES;
二、提示有多个按钮
[[self window] makeKeyAndVisible];
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"不好啦!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"fuck1",@"fuck2",@"fuck3",@"fuck4", nil];
[alter show];
[alter release];
return YES;
三、判断点了哪一个按钮(通过alter的委托)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[self window] makeKeyAndVisible];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"不好啦!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"fuck1",@"fuck2",@"fuck3",@"fuck4", nil];
[alert show];
[alert release];
return YES;
}
- (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, nil];
[alert show];
[alert release];
}
四、手动取消对话框
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[self window] makeKeyAndVisible];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"不好啦!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"fuck1",@"fuck2",@"fuck3",@"fuck4", nil];
[alert show];
[alert release];
return YES;
}
- (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, nil];
[alert show];
[alert dismissWithClickedButtonIndex:0 animated:YES];
[alert release];
}
五、添加子视图
在为UIAlertView对象太添加子视图的过程中,有点是需要注意的地方,如果删除按钮,也就是取消UIAlerView视图中所有的按钮的时候,可能会导致整个显示结构失衡。按钮占用的空间不会消失,我们也可以理解为这些按钮没有真正的删除,仅仅是他不可见了而已。如果在UIAlertview对象中仅仅用来显示文本,那么,可以在消息的开头添加换行符(@"\n)有助于平衡按钮底部和顶部的空间。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[self window] makeKeyAndVisible];
/*
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"不好啦!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"fuck1",@"fuck2",@"fuck3",@"fuck4", nil];
[alert show];
[alert release];
*/
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"给UIAlertView添加自定义视图"
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];
return YES;
}
六、修改展现属性
UIAlertView默认情况下所有的text是居中对齐的。 那如果需要将文本向左对齐或者添加其他控件比如输入框时该怎么办呢? 不用担心, iPhone SDK还是很灵活的, 有很多delegate消息供调用程序使用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[self window] makeKeyAndVisible];
/*
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"不好啦!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"fuck1",@"fuck2",@"fuck3",@"fuck4", nil];
[alert show];
[alert release];
*/
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"添加自定义视图"
message:nil
delegate:self
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];
return YES;
}
-(void) willPresentAlertView:(UIAlertView *)alertView
{
for( UIView * view in alertView.subviews )
{
if( [view isKindOfClass:[UILabel class]] )
{
UILabel* label = (UILabel*) view;
label.textAlignment=UITextAlignmentLeft;
}
}
}