重溫-UIAlertView

參考: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];
    
效果圖.png
  • 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;
            }
      }
}
效果.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末屿良,一起剝皮案震驚了整個濱河市圈澈,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌尘惧,老刑警劉巖康栈,帶你破解...
    沈念sama閱讀 217,907評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡啥么,警方通過查閱死者的電腦和手機登舞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來悬荣,“玉大人菠秒,你說我怎么就攤上這事∮缥酰” “怎么了稽煤?”我有些...
    開封第一講書人閱讀 164,298評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長囚戚。 經(jīng)常有香客問我酵熙,道長,這世上最難降的妖魔是什么驰坊? 我笑而不...
    開封第一講書人閱讀 58,586評論 1 293
  • 正文 為了忘掉前任匾二,我火速辦了婚禮,結(jié)果婚禮上拳芙,老公的妹妹穿的比我還像新娘察藐。我一直安慰自己,他們只是感情好舟扎,可當我...
    茶點故事閱讀 67,633評論 6 392
  • 文/花漫 我一把揭開白布分飞。 她就那樣靜靜地躺著,像睡著了一般睹限。 火紅的嫁衣襯著肌膚如雪譬猫。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,488評論 1 302
  • 那天羡疗,我揣著相機與錄音染服,去河邊找鬼。 笑死叨恨,一個胖子當著我的面吹牛柳刮,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播痒钝,決...
    沈念sama閱讀 40,275評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼秉颗,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了午乓?” 一聲冷哼從身側(cè)響起站宗,我...
    開封第一講書人閱讀 39,176評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎益愈,沒想到半個月后梢灭,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體夷家,經(jīng)...
    沈念sama閱讀 45,619評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,819評論 3 336
  • 正文 我和宋清朗相戀三年敏释,在試婚紗的時候發(fā)現(xiàn)自己被綠了库快。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,932評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡钥顽,死狀恐怖义屏,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蜂大,我是刑警寧澤闽铐,帶...
    沈念sama閱讀 35,655評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站奶浦,受9級特大地震影響兄墅,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜澳叉,卻給世界環(huán)境...
    茶點故事閱讀 41,265評論 3 329
  • 文/蒙蒙 一隙咸、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧成洗,春花似錦五督、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至遥椿,卻和暖如春误证,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背修壕。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留遏考,地道東北人慈鸠。 一個月前我還...
    沈念sama閱讀 48,095評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像灌具,于是被迫代替她去往敵國和親青团。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,884評論 2 354

推薦閱讀更多精彩內(nèi)容