一、基礎用法
[[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:@"給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: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;
}
}
}