參考:http://www.cnblogs.com/xmqios/p/3477461.html
一個(gè)小bug
項(xiàng)目中用到了右滑返回框架(KKNavigationController),由于提交訂單時(shí)候彈出UIAlertView分別跳轉(zhuǎn)到訂單詳情和支付頁(yè)面,我的操作如下:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0){
//跳轉(zhuǎn)到支付界面
}
if(buttonIndex == 1){
//跳轉(zhuǎn)到訂單詳情界面
}
}
這些寫的問(wèn)題是:當(dāng)調(diào)整到訂單詳情或者支付界面后,右滑返回時(shí)出現(xiàn)黑色界面如下:

導(dǎo)致這個(gè)問(wèn)題的原因很簡(jiǎn)單,界面跳轉(zhuǎn)到下級(jí)頁(yè)面時(shí)UIAlertView還沒(méi)有消失,改為如下即OK:
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0){
//跳轉(zhuǎn)到支付界面
}
if(buttonIndex == 1){
//跳轉(zhuǎn)到訂單詳情界面
}
}
下面仔細(xì)探討一下UIAlertView的生命周期以及對(duì)應(yīng)的方法。
UIAlertView詳解
屬性
- title
獲取或著設(shè)置UIAlertView上的標(biāo)題
- message
獲取或設(shè)置UIAlertView上的消息
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
alertView.title = @"T";
alertView.message = @"M";
[alertView show];

- numberOfbuttons(只讀)
返回UIAlertView上有多少按鈕
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
NSLog(@"%d",alertView.numberOfButtons);//2個(gè)
[alertView show];
- cancelButtonIndex
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"請(qǐng)選擇一個(gè)按鈕:" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"按鈕一", @"按鈕二", @"按鈕三",nil];
[alert show];
NSLog(@"UIAlertView中取消按鈕的角標(biāo)是%d",alert.cancelButtonIndex);//0
-
alertViewStyle
-
UIAlertViewStyleLoginAndPasswordInput
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"產(chǎn)品信息展示" message:p.name delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil]; alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; [alert show];效果
-
-
UIAlertViewStylePlainTextInput
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"產(chǎn)品信息展示" message:p.name delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil]; alert.alertViewStyle = UIAlertViewStylePlainTextInput; [alert show];

-
UIAlertViewStyleSecureTextInput
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"產(chǎn)品信息展示" message:p.name delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil]; alert.alertViewStyle = UIAlertViewStyleSecureTextInput; [alert show];效果
代理
//當(dāng)點(diǎn)擊UIAlertView上的按鈕時(shí),就會(huì)調(diào)用,并且當(dāng)方法調(diào)完后,UIAlertView會(huì)自動(dòng)消失。
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
//當(dāng)UIAlertView即將出現(xiàn)的時(shí)候調(diào)用
- (void)willPresentAlertView:(UIAlertView *)alertView;
//當(dāng)UIAlertView完全出現(xiàn)的時(shí)候調(diào)用
- (void)didPresentAlertView:(UIAlertView *)alertView;
// 當(dāng)UIAlertView即將消失的時(shí)候調(diào)用
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex;
//當(dāng)UIAlertView完全消失的時(shí)候調(diào)用
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
注意: UIAlertView調(diào)用show顯示出來(lái)的時(shí)候,系統(tǒng)會(huì)自動(dòng)強(qiáng)引用它,不會(huì)被釋放。
為UIAlertView添加子視圖
在為UIAlertView對(duì)象里添加子視圖的過(guò)程中,有點(diǎn)是需要注意的地方,如果刪除按鈕,也就是取消UIAlerView視圖中所有的按鈕的時(shí)候,可能會(huì)導(dǎo)致整個(gè)顯示結(jié)構(gòu)失衡。按鈕占用的空間不會(huì)消失,我們也可以理解為這些按鈕沒(méi)有真正的刪除,僅僅是他不可見(jiàn)了而已。如果在UIAlertview對(duì)象中僅僅用來(lái)顯示文本,那么,可以在消息的開(kāi)頭添加換行符(@"\n)有助于平衡按鈕底部和頂部的空間。
下面的代碼用來(lái)演示如何為UIAlertview對(duì)象添加子視圖的方法。
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"請(qǐng)等待" 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];

UIAlertView小例子
UIAlertView默認(rèn)情況下所有的text是居中對(duì)齊的。 那如果需要將文本向左對(duì)齊或者添加其他控件比如輸入框時(shí)該怎么辦呢? 不用擔(dān)心, iPhone SDK還是很靈活的, 有很多delegate消息供調(diào)用程序使用。 所要做的就是在
- (void)willPresentAlertView:(UIAlertView *)alertView
中按照自己的需要修改或添加即可, 比如需要將消息文本左對(duì)齊,下面的代碼即可實(shí)現(xiàn):
-(void) willPresentAlertView:(UIAlertView *)alertView
{
for( UIView * view in alertView.subviews )
{
if( [view isKindOfClass:[UILabel class]] )
{
UILabel* label = (UILabel*) view;
label.textAlignment=UITextAlignmentLeft;
}
}
}


