參考:http://www.cnblogs.com/xmqios/p/3477461.html
一個小bug
項目中用到了右滑返回框架(KKNavigationController),由于提交訂單時候彈出UIAlertView分別跳轉(zhuǎn)到訂單詳情和支付頁面,我的操作如下:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0){
//跳轉(zhuǎn)到支付界面
}
if(buttonIndex == 1){
//跳轉(zhuǎn)到訂單詳情界面
}
}
這些寫的問題是:當調(diào)整到訂單詳情或者支付界面后,右滑返回時出現(xiàn)黑色界面如下:
導致這個問題的原因很簡單,界面跳轉(zhuǎn)到下級頁面時UIAlertView還沒有消失瘾带,改為如下即OK:
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0){
//跳轉(zhuǎn)到支付界面
}
if(buttonIndex == 1){
//跳轉(zhuǎn)到訂單詳情界面
}
}
下面仔細探討一下UIAlertView的生命周期以及對應(yīng)的方法蔽午。
UIAlertView詳解
屬性
- title
獲取或著設(shè)置UIAlertView上的標題
- 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個
[alertView show];
- cancelButtonIndex
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"請選擇一個按鈕:" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"按鈕一", @"按鈕二", @"按鈕三",nil];
[alert show];
NSLog(@"UIAlertView中取消按鈕的角標是%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];
代理
//當點擊UIAlertView上的按鈕時下愈,就會調(diào)用,并且當方法調(diào)完后粘衬,UIAlertView會自動消失项玛。
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
//當UIAlertView即將出現(xiàn)的時候調(diào)用
- (void)willPresentAlertView:(UIAlertView *)alertView;
//當UIAlertView完全出現(xiàn)的時候調(diào)用
- (void)didPresentAlertView:(UIAlertView *)alertView;
// 當UIAlertView即將消失的時候調(diào)用
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex;
//當UIAlertView完全消失的時候調(diào)用
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
注意: UIAlertView調(diào)用show顯示出來的時候貌笨,系統(tǒng)會自動強引用它,不會被釋放襟沮。
為UIAlertView添加子視圖
在為UIAlertView對象里添加子視圖的過程中锥惋,有點是需要注意的地方,如果刪除按鈕开伏,也就是取消UIAlerView視圖中所有的按鈕的時候净刮,可能會導致整個顯示結(jié)構(gòu)失衡。按鈕占用的空間不會消失硅则,我們也可以理解為這些按鈕沒有真正的刪除,僅僅是他不可見了而已株婴。如果在UIAlertview對象中僅僅用來顯示文本怎虫,那么,可以在消息的開頭添加換行符(@"\n)有助于平衡按鈕底部和頂部的空間困介。
下面的代碼用來演示如何為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];
UIAlertView小例子
UIAlertView默認情況下所有的text是居中對齊的。 那如果需要將文本向左對齊或者添加其他控件比如輸入框時該怎么辦呢座哩? 不用擔心徒扶, iPhone SDK還是很靈活的, 有很多delegate消息供調(diào)用程序使用根穷。 所要做的就是在
- (void)willPresentAlertView:(UIAlertView *)alertView
中按照自己的需要修改或添加即可姜骡, 比如需要將消息文本左對齊,下面的代碼即可實現(xiàn):
-(void) willPresentAlertView:(UIAlertView *)alertView
{
for( UIView * view in alertView.subviews )
{
if( [view isKindOfClass:[UILabel class]] )
{
UILabel* label = (UILabel*) view;
label.textAlignment=UITextAlignmentLeft;
}
}
}