今天在研究UIActionSheet 直接把代碼放到viewDidLoad中來執行,費了半天的勁總是出現問題,也懷疑過是不是xcode的問題,後來發現平時用都是放到一個button的方法裡來操作,於是有個觀點產生UIActionSheet必須配合動作時才有效果。於是去查看開發文檔,上面有句話也驗證了觀點:Action sheets display a set of buttons representing several alternative choices to complete a task initiated by the user.
官方文檔:https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/UIKitUICatalog/UIActionSheet.html
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"first ActionSheet" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"刪除" otherButtonTitles:@"保持", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[actionSheet showInView:self.view];
#pragma mark---實現UIActionSheetDelegate協議
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
[self showAlert:@"確定"];
}else if (buttonIndex == 1) {
[self showAlert:@"第一項"];
}else if(buttonIndex == 2) {
[self showAlert:@"第二項"];
}else if(buttonIndex == 3) {
[self showAlert:@"取消"];
}
NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
NSLog(@"buttonTitle = %@",buttonTitle);
}
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;//設置樣式
參數解釋:
cancelButtonTitle destructiveButtonTitle是系統自動的兩項。
otherButtonTitles是自己定義的項,注意,最後一個參數要是nil。
[actionSheet showInView:self.view];這行語句的意思是在當前view顯示Action sheet。當然還可以用其他方法顯示Action sheet。
看到那個紅色的按鈕沒?那是ActionSheet支持的一種所謂的銷毀按鈕,對某戶的某個動作起到警示作用,
比如永久性刪除一條消息或圖像時。如果你指定了一個銷毀按鈕他就會以紅色高亮顯示:
actionSheet.destructiveButtonIndex=1;
與導航欄類似,操作表單也支持三種風格 :
UIActionSheetStyleDefault //默認風格:灰色背景上顯示白色文字
UIActionSheetStyleBlackTranslucent //透明黑色背景,白色文字
UIActionSheetStyleBlackOpaque //純黑背景,白色文字
用法:
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;//設置樣式