警告框(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];
}