警告框和動作表(IOS開發)
警告框(AlertView)時模態的,不關閉它就不能做其他事情,所以不是以下幾種情況不應該隨便使用。
1、應用不能繼續運行。
如記憶體不足,沒有網路。一般只需要一個按鈕。
2、詢問另一個解決方案。
不能運行時,詢問是否可以用3G網路。
3、詢問對操作的授權。
涉及到訪問隱私資訊的時候,需要使用者授權,如位置、相簿等。
動作表(ActionSheet)可以給使用者提供多個選擇。可以利用它將某個圖片發給新浪微博或者Facebook平台。
/ 實現UIAlertViewDelegate// 這個委託其實沒有用到,就當練練手,因為警告視窗有兩個按鈕索引// No為0,Yes為1-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSLog(@"buttonIndex = %li", (long)buttonIndex);}// 實現UIActionSheetDelegate// 這個委託也沒有實際意義,就是在輸出命令視窗輸出按下的索引數,以實現響應- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ NSLog(@"buttonIndex = %li", (long)buttonIndex);}- (IBAction)testAlertView:(id)sender { // 警告框在上文已敘述 // delegate 參數用於設定該警告視窗的委派物件 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message: @"Alert text goes here" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; [alertView show]; }- (IBAction)testActionSheet:(id)sender { // cancelButtonTitle 設定取消標題 // destructiveButtonTile 設定破壞型按鈕,只能有一個在最上面 UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消"destructiveButtonTitle:@"破壞性按鈕" otherButtonTitles:@"新浪微博", nil]; // 設定為自動樣式 actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic; [actionSheet showInView:self.view];}