iPhone開(kāi)發(fā)中了解UIALertView和UIActionSheet應(yīng)用
iPhone開(kāi)發(fā)中了解UIALertView和UIActionSheet應(yīng)用是本文要學(xué)習(xí)的內(nèi)容,主要介紹UIALertView和UIActionSheet的應(yīng)用,先來(lái)看詳細(xì)內(nèi)容。
1:構(gòu)建一個(gè)簡(jiǎn)單的警告框:
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"xingchen"
 - message:@"message"
 - delegate:nil
 - cancelButtonTitle:@"OK"
 - otherButtonTitles:nil];
 - [alert show];
 - [alert release];
 
這里,僅僅是現(xiàn)實(shí)一個(gè)按鈕的對(duì)話框,以模態(tài)的形式運(yùn)行。
2:當(dāng)然,我們可以在一個(gè)alertView中添加多個(gè)按鈕,參考代碼如下所示:
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"xingchen"
 
- message:@"message"
 - delegate:nil
 - cancelButtonTitle:@"OK"
 - otherButtonTitles:@"FirstButton",@"SecondButton",
 - nil];
 - [alert show];
 - [alert release];
 
3:判斷單擊來(lái)那個(gè)按鈕,并根據(jù)不同的按鈕觸發(fā)不同的事件:
alertView有一個(gè)委托(UIAlertViewDelegate),我們可以繼承他,然后調(diào)用該委托來(lái)處理事件
參考代碼
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"xingchen"
 - message:@"message" delegate:self
 - cancelButtonTitle:@"OK"
 - otherButtonTitles:@"FirstButton",
 - @"SecondButton",nil];
 - [alert show];
 - [alert release];
 - //delegate
 - -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(int)index
 - {
 - NSString *tt = [[NSString alloc] initWithFormat:@"%d",index];
 - UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"xingchen"
 - message:tt
 - delegate:self
 - cancelButtonTitle:@"OK"
 - otherButtonTitles:nil];
 - [alert show];
 - [alert release];
 - }
 
4:手動(dòng)的取消對(duì)話框
參考代碼:
- [alert dismissWithClickedButtonIndex:0 animated:YES];
 
#p#
5:為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ì)象添加子視圖的方法。
- -(IBAction)showAlert
 - {
 - UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"xingchen" 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];
 - }
 
顯示效果如下所示:
6:UIActionSheet
UIActionSheet也是一個(gè)模態(tài)對(duì)話框,提供類似于菜單的功能,供用戶進(jìn)行選擇,參考代碼如下所示:
- -(IBAction)showAlert
 - {
 - UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"ActionSheetTitle"
 - delegate:nil
 - cancelButtonTitle:@"Cancel"
 - destructiveButtonTitle:nil
 - otherButtonTitles:@"FirstButton",@"SecondButton",@"thirdButton",nil];
 - [sheet showInView:[self view]];
 - [sheet release];
 - }
 - [sheet showInView:[self view]];
 
表示該UIActionsheet在那個(gè)視圖里面顯示。
他有一個(gè)委托是:UIActionSheetDelegate
委托的函數(shù):
- @protocol UIActionSheetDelegate <NSObject>
 - @optional
 - // Called when a button is clicked. The view will be automatically dismissed after this call returns
 - - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
 - // Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.
 - // If not defined in the delegate, we simulate a click in the cancel button
 - - (void)actionSheetCancel:(UIActionSheet *)actionSheet;
 - - (void)willPresentActionSheet:(UIActionSheet *)actionSheet;
 - // before animation and showing view
 - - (void)didPresentActionSheet:(UIActionSheet *)actionSheet;
 - // after animation
 - - (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex;
 - // before animation and hiding view
 - - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex;
 - // after animation
 - @end
 
7:Please wait ;向用戶顯示進(jìn)度。
等待是計(jì)算機(jī)用戶體驗(yàn)的一個(gè)部分,iphone也需要在某些情況下告知用戶你所需要的數(shù)據(jù)正在加載過(guò)程中,請(qǐng)用戶耐心等待,并需要讓用戶看到iphone當(dāng)前確實(shí)在運(yùn)行而不是已經(jīng)死機(jī)。在iphone有2中有兩種方式來(lái)達(dá)到該效果
- UIActivityIndicatorView 和UIActionSheet
 
8:UIProgressBar
一個(gè)顯示進(jìn)度的進(jìn)度條控件,他可以讓用戶看到程序工作的進(jìn)度,指示任務(wù)完成的程度。
9:使用網(wǎng)絡(luò)指示器:
- UIApplication *app = [UIApplication sharedApplication];
 - app.networkActivityIndicatorVisible = !app.networkActivityIndicatorVisible;
 
小結(jié):iPhone開(kāi)發(fā)中了解UIALertView和UIActionSheet應(yīng)用的內(nèi)介紹完了,希望本文對(duì)你有所幫助!



















 
 
 
 
 
 
 