iOS界面編程-UIActionSheet

UIActionSheet

一、介紹

   UIActionSheet在ios8之後就被棄用了,在ios8及以後的版本中,推薦使用UIAlertController並指定preferredStyle爲 UIAlertControllerStyleActionSheet.我們可以使用UIActionSheet去提醒用戶去處理給定的任務,也可以使用動作列表去提醒用戶去確認一個潛在的危險動作。動作列表包含選擇的標題以及一個或者多個按鈕,

二、相關屬性及方法

- (instancetype)initWithTitle:(nullable NSString *)title delegate:(nullable id<UIActionSheetDelegate>)delegate cancelButtonTitle:(nullable NSString *)cancelButtonTitle destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle otherButtonTitles:(nullable NSString *)otherButtonTitles, ... 


@property(nullable,nonatomic,weak) id<UIActionSheetDelegate> delegate;

@property(nonatomic,copy) NSString *title;

@property(nonatomic) UIActionSheetStyle actionSheetStyle; // default is UIActionSheetStyleAutomatic. ignored if alert is visible

- (NSInteger)addButtonWithTitle:(nullable NSString *)title;    // returns index of button. 0 based.

- (nullable NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;

@property(nonatomic,readonly) NSInteger numberOfButtons;

@property(nonatomic) NSInteger cancelButtonIndex;      // if the delegate does not implement -actionSheetCancel:, we pretend this button was clicked on. default is -1

@property(nonatomic) NSInteger destructiveButtonIndex;        // sets destructive (red) button. -1 means none set. default is -1. ignored if only one button

@property(nonatomic,readonly) NSInteger firstOtherButtonIndex; // -1 if no otherButtonTitles or initWithTitle:... not used

@property(nonatomic,readonly,getter=isVisible) BOOL visible;


// show a sheet animated. you can specify either a toolbar, a tab bar, a bar button item or a plain view. We do a special animation if the sheet rises from

// a toolbar, tab bar or bar button item and we will automatically select the correct style based on the bar style. if not from a bar, we use

// UIActionSheetStyleDefault if automatic style set

- (void)showFromToolbar:(UIToolbar *)view;

- (void)showFromTabBar:(UITabBar *)view;

- (void)showFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated NS_AVAILABLE_IOS(3_2);

- (void)showFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated NS_AVAILABLE_IOS(3_2);

- (void)showInView:(UIView *)view;


// hides alert sheet or popup. use this method when you need to explicitly dismiss the alert.

// it does not need to be called if the user presses on a button

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;

三、實際例子


-(void)showActionButtonClicked:(id)sender{
  UIActionSheet *testAction =  [ [UIActionSheet alloc]initWithTitle:@"測試中" delegate:self cancelButtonTitle:@"cancle" destructiveButtonTitle:@"destructor" otherButtonTitles:@"sr", nil];
    [testAction showInView:self.view];
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章