iPhone開發中了解UIALertView和UIActionSheet應用
iPhone開發中了解UIALertView和UIActionSheet應用是本文要學習的內容,主要介紹UIALertView和UIActionSheet的應用,先來看詳細內容。
1:構建一個簡單的警告框:
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"xingchen"
- message:@"message"
- delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alert show];
- [alert release];
這里,僅僅是現實一個按鈕的對話框,以模態的形式運行。
2:當然,我們可以在一個alertView中添加多個按鈕,參考代碼如下所示:
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"xingchen"
- message:@"message"
- delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:@"FirstButton",@"SecondButton",
- nil];
- [alert show];
- [alert release];
3:判斷單擊來那個按鈕,并根據不同的按鈕觸發不同的事件:
alertView有一個委托(UIAlertViewDelegate),我們可以繼承他,然后調用該委托來處理事件
參考代碼
- 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:手動的取消對話框
參考代碼:
- [alert dismissWithClickedButtonIndex:0 animated:YES];
#p#
5:為UIAlertView添加子視圖
在為UIAlertView對象太添加子視圖的過程中,有點是需要注意的地方,如果刪除按鈕,也就是取消UIAlerView視圖中所有的按鈕的時候,可能會導致整個顯示結構失衡。 按鈕占用的空間不會消失,我們也可以理解為這些按鈕沒有真正的刪除,僅僅是他不可見了而已。如果在UIAlertview對象中僅僅用來顯示文本,那么,可以在消息的開頭添加換行符(@"\n)有助于平衡按鈕底部和頂部的空間。
下面的代碼用來演示如何為UIAlertview對象添加子視圖的方法。
- -(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也是一個模態對話框,提供類似于菜單的功能,供用戶進行選擇,參考代碼如下所示:
- -(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在那個視圖里面顯示。
他有一個委托是:UIActionSheetDelegate
委托的函數:
- @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 ;向用戶顯示進度。
等待是計算機用戶體驗的一個部分,iphone也需要在某些情況下告知用戶你所需要的數據正在加載過程中,請用戶耐心等待,并需要讓用戶看到iphone當前確實在運行而不是已經死機。在iphone有2中有兩種方式來達到該效果
- UIActivityIndicatorView 和UIActionSheet
8:UIProgressBar
一個顯示進度的進度條控件,他可以讓用戶看到程序工作的進度,指示任務完成的程度。
9:使用網絡指示器:
- UIApplication *app = [UIApplication sharedApplication];
- app.networkActivityIndicatorVisible = !app.networkActivityIndicatorVisible;
小結:iPhone開發中了解UIALertView和UIActionSheet應用的內介紹完了,希望本文對你有所幫助!