[iOS] UIActionSheet 的使用方法

Posted in :

UIActionSheet 是一個由下往上彈出的選單.

在使用 UIActionSheet 之前,請先為您的程式加上代理

@interface UIActionSheetViewController : UIViewController<UIActionSheetDelegate> {
 
}

在按鈕事件加入產生 UIActionSheet 的程式碼。

 UIActionSheet *actionSheet = [[UIActionSheet alloc] 
 initWithTitle:@"title,nil时不显示" 
 delegate:self 
 cancelButtonTitle:@"取消" 
 destructiveButtonTitle:@"确定" 
 otherButtonTitles:@"第一项", @"第二项",nil]; 
 actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque; 
 [actionSheet showInView:self.view];

 

//也可以透過此方式新增按鈕

[actionSheet addButtonWithTitle:@”NewButton”];

 

所觸發的按鈕事件寫法就與 Alerts 一模一樣了,同都是呼叫一個內建函式,使用索引 Index 並配合按鈕標題來做判斷。

//判斷ActionSheet按鈕事件
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
 
 //將按鈕的Title當作判斷的依據
 NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex];
 
 if([title isEqualToString:@"Title1"]) {
 // code here.
 }
}

和 UIActionSheet 類似的是

 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"选择" message:@"请选择一个数!" delegate:weakSelf cancelButtonTitle:@"取消" otherButtonTitles:nil];
 for (int index = 0; index < 100; index++) {
 [alertView addButtonWithTitle:[NSString stringWithFormat:@"%i", index]];
 }
 [alertView show];

 

UIAlertView有如下4中樣式:

– UIAlertViewStyleDefault 默認樣式,沒有熟人框
– UIAlertViewStyleSecureTextInput 有一個密碼輸入框
– UIAlertViewStylePlainTextInput 有一個普通文本輸入框
– UIAlertViewStyleLoginAndPasswordInput 有一個普通文本輸入框和一個密碼輸入框


UIAlertView有如下幾個代理方法
在點擊按鈕的時候觸發,buttonIndex為點擊的按鈕序號
-(void)alertView:(UIAlertView* )alertView clickedButtonAtIndex:(NSInteger)buttonIndex

在將要顯示的時候觸發
-(void)willPresentAlertView:(UIAlertView )alertView

在已經顯示的時候觸發
-(void)didPresentAlertView:(UIAlertView )alertView

在將要消失的時候觸發
-(void)alertView:(UIAlertView )alertView willDismissWithButtonIndex:(NSInteger)buttonIndex

在已經消失的時候觸發
-(void)alertView:(UIAlertView )alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

在編輯文本框後觸發
-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView )alertView NS_DEPRECATED_IOS(2_0, 9_0);

 

 

執行出來的效果:


相關文章:

iOS 弹出框总结
http://www.jianshu.com/p/4d1ca0d9a6f1

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *