`
king_tt
  • 浏览: 2258713 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

【移动开发】操作表单_UIActionSheet

 
阅读更多

概述

如果要和UIActionSheet对象交互,那么UIViewController类的h文件需要遵循UIActionSheetDelegate协议。示例代码:

@interface BIDViewController : UIViewController <UIActionSheetDelegate>

并且,需要在UIViewController类的m文件中构造一个UIActionSheet对象,同时实现协议的方法。示例代码:

- (IBAction)buttonPressed:(id)sender {
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:@"Are you sure?"
                                  delegate:self
                                  cancelButtonTitle:@"No Way!"
                                  destructiveButtonTitle:@"Yes, I’m Sure!"
                                  otherButtonTitles:nil];
    [actionSheet showInView:self.view];
}

delegate参数就是UIActionSheet的委托对象,在这个例子中,传入的selt就是BIDViewController对象本身,因为它实现了UIActionSheetDelegate协议。

调用showInView:方法,此处传入BIDViewController对象的view实例,从而可以在BIDViewController的页面上显示出UIActionSheet。

- (void)actionSheet:(UIActionSheet *)actionSheet
didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != [actionSheet cancelButtonIndex])
    {
        NSString *msg = nil;
        
        if (self.nameField.text.length > 0)
            msg = [NSString stringWithFormat:
                   @"You can breathe easy, %@, everything went OK.",
                   self.nameField.text];
        else
            msg = @"You can breathe easy, everything went OK.";
        
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Something was done"
                              message:msg
                              delegate:self
                              cancelButtonTitle:@"Phew!"
                              otherButtonTitles:nil];
        [alert show];
    }
}
通过buttonIndex参数判断点击的是哪个按钮。


该协议的描述。

The UIActionSheetDelegate protocol defines the methods a delegate of a UIActionSheet object should implement. The delegate implements the button actions and any other custom behavior. Some of the methods defined in this protocol are optional.

声明了一些UIActionSheet的委托对象需要实现的一些方法。


该协议的源码。

//
//  UIActionSheet.h
//  UIKit
//
//  Copyright 2010-2012, Apple Inc. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKitDefines.h>
#import <UIKit/UITextField.h>
#import <UIKit/UIView.h>


@protocol UIActionSheetDelegate;
@class UILabel, UIToolbar, UITabBar, UIWindow, UIBarButtonItem, UIPopoverController;

typedef NS_ENUM(NSInteger, UIActionSheetStyle) {
    UIActionSheetStyleAutomatic        = -1,       // take appearance from toolbar style otherwise uses 'default'
    UIActionSheetStyleDefault          = UIBarStyleDefault,
    UIActionSheetStyleBlackTranslucent = UIBarStyleBlackTranslucent,
    UIActionSheetStyleBlackOpaque      = UIBarStyleBlackOpaque,
};

NS_CLASS_AVAILABLE_IOS(2_0) @interface UIActionSheet : UIView {
@private
    id <UIActionSheetDelegate> _delegate;
    UILabel   *_titleLabel;
    NSAttributedString *_attributedTitleString;
    UILabel   *_subtitleLabel;
    UILabel   *_bodyTextLabel;
    UILabel   *_taglineTextLabel;
    CGFloat    _startY;
    id         _context;
    NSInteger  _cancelButton;
    NSInteger  _defaultButton;
    NSInteger  _firstOtherButton;
    UIToolbar *_toolbar;
    UIWindow  *_originalWindow;
    UIWindow  *_dimWindow;
    NSInteger  _suspendTag;
    NSInteger  _dismissButtonIndex;
    CGFloat    _bodyTextHeight;
    NSMutableArray *_buttons;
    NSMutableArray *_buttonsInTable;
    NSMutableArray *_textFields;
    UIView *_keyboard;
    UIView *_table;
    UIView *_buttonTableView;
    UIView *_dimView;
    UIPopoverController * _popoverController;
    CGFloat _fontSizeInTableView; // font size for 'complex' buttons (phone numbers || doc interaction controller)
    CGFloat _iconOffset; // doc interaction controller app icon
    CGFloat _labelOffset; // phone numbers layout 
    CGFloat _labelWidth;
    CGFloat _titleWidth;
    BOOL _oldIgnoreTapsValue;
    
    struct {
        unsigned int numberOfRows:7;
        unsigned int delegateAlertSheetButtonClicked:1;
        unsigned int delegateDidPresentAlertSheet:1;
        unsigned int delegateDidDismissAlertSheet:1;
        unsigned int hideButtonBar:1;
        unsigned int alertStyle:3;
        unsigned int dontDimBackground:1;
        unsigned int dismissSuspended:1;
        unsigned int dontBlockInteraction:1;
        unsigned int sheetWasPoppedUp:1;
        unsigned int sheetWasShown:1;
        unsigned int animating:1;
        unsigned int hideWhenDoneAnimating:1;
        unsigned int layoutWhenDoneAnimating:1;
        unsigned int titleMaxLineCount:2;
        unsigned int bodyTextMaxLineCount:3;
        unsigned int runsModal:1;
        unsigned int runningModal:1;
        unsigned int addedTextView:1;
        unsigned int addedTableShadows:1;
        unsigned int showOverSBAlerts:1;
        
        unsigned int showMinTableContent:1;
        unsigned int bodyTextTruncated:1;
        unsigned int orientation:3;
        unsigned int popupFromPoint:1;
        unsigned int inPopover:1;
        unsigned int delegateBodyTextAlignment:1;
        unsigned int delegateClickedButtonAtIndex:1;
        unsigned int delegateClickedButtonAtIndex2:1;
        unsigned int delegateCancel:1;
        unsigned int delegateCancel2:1;
        unsigned int delegateWillPresent:1;
        unsigned int delegateWillPresent2:1;
        unsigned int delegateDidPresent:1;
        unsigned int delegateDidPresent2:1;
        unsigned int delegateWillDismiss:1;
        unsigned int delegateWillDismiss2:1;
        unsigned int delegateDidDismiss:1;
        unsigned int delegateDidDismiss2:1;
        unsigned int dontCallDismissDelegate:1;
        unsigned int useAutomaticKB:1;
        unsigned int twoColumnsLayoutMode:7; // one column || even width (leaves empty space) || first button wider || last button wider
        unsigned int threeColumnsLayoutMode:7; // one column || even width (leaves empty space) || first button wider || last button wider
        unsigned int shouldHandleFirstKeyUpEvent:1; // when presenting with hardware KB we have to handle the first key up event we get so we don't end up repeating the last key
        unsigned int cancelWhenDoneAnimating:1;
        unsigned int useThreePartButtons:1; // phone numbers layout
        unsigned int useTwoPartButtons:1; // doc interaction layout
        unsigned int displaySelectedButtonGlyph:1;
        int indexOfSelectedButton:7; // default -1 (no checkmark) otherwise will display a checkbox (this for the airtunes action sheet)
        unsigned int useCustomSelectedButtonGlyph:1;
    } _modalViewFlags;
    UIActionSheetStyle _actionSheetStyle;
    UIImage *_selectedButtonGlyphImage;
    UIImage *_selectedButtonGlyphHighlightedImage;
    UIImageView *_shadowImageView;
}

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

@property(nonatomic,assign) id<UIActionSheetDelegate> delegate;    // weak reference
@property(nonatomic,copy) NSString *title;
@property(nonatomic) UIActionSheetStyle actionSheetStyle; // default is UIActionSheetStyleAutomatic. ignored if alert is visible

// adds a button with the title. returns the index (0 based) of where it was added. buttons are displayed in the order added except for the
// destructive and cancel button which will be positioned based on HI requirements. buttons cannot be customized.
- (NSInteger)addButtonWithTitle:(NSString *)title;    // returns index of button. 0 based.
- (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 butto 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;

@end


@protocol UIActionSheetDelegate <NSObject>
@optional

// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;

// Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.
// If not defined in the delegate, we simulate a click in the cancel button
- (void)actionSheetCancel:(UIActionSheet *)actionSheet;

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet;  // before animation and showing view
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet;  // after animation

- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex; // before animation and hiding view
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex;  // after animation

@end


参考资料

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIActionSheet_Class/

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIModalViewDelegate_Protocol/

https://developer.apple.com/library/IOS/documentation/UserExperience/Conceptual/UIKitUICatalog/UIActionSheet.html

分享到:
评论

相关推荐

    UIActionSheet

    在iOS开发中,UIActionSheet是苹果提供的一个控件,用于在用户界面中展示一系列可选操作,通常出现在屏幕底部或底部弹出。这个控件是UIKit框架的一部分,主要用于处理用户的交互事件,尤其是在移动设备上提供一种...

    UIActionSheet示例代码

    在iOS开发中,UIActionSheet是用户界面中一个重要的组件,它用于展示一系列可供用户选择的选项,通常在用户需要做出决策或者执行某些操作时出现。`UIActionSheet`类在早期的iOS版本中被广泛使用,但在iOS 8之后,...

    iOS开发6:UIActionSheet与UIAlertView

    在iOS开发中,UIActionSheet和UIAlertView是两种常用的用户交互组件,它们用于向用户提供警告、确认或选择。在iOS 5.1版本中,这两种组件是进行用户界面交互的重要工具。 UIActionSheet通常用于呈现一组有限的选项...

    UIPickerView+UIActionSheet

    在iOS开发中,UIPickerView和UIActionSheet是两种常见的用户界面组件,它们分别用于不同的交互场景。`UIPickerView`通常用于展示一系列可滚动的选择项,比如日期选择、时间选择或者一组分类选项。而`UIActionSheet`...

    Android 仿iPhone UIActionSheet控件效果很好用

    在Android开发中,为了提供与iOS相似的用户体验,开发者经常需要实现类似iPhone的UI组件。在iOS中,UIActionSheet是一种常见的控件,用于展示一系列可供用户选择的选项,通常在底部弹出。而在Android中,我们可以...

    iOS中的表单按钮选项UIActionSheet常用方法整理

    在实际开发中,UIActionSheet通常结合委托方法来处理用户的操作,确保程序逻辑的正确执行。需要注意的是,从iOS 8开始,苹果推荐使用UIAlertController替代UIActionSheet,因为UIAlertController提供了更丰富的...

    ios应用源码之仿uialertview和uiactionsheet的一个效果 2018127

    `UIAlertController`可以展示警告信息,同时允许添加多个操作按钮,而`UIActionSheet`通常用于在多个选项之间提供选择。 在描述中提到的源码,可能是开发者为了重现`UIAlertController`和`UIActionSheet`的效果,但...

    UIAlertView,UIActionSheet,UIAlertController封装,OS任何系统都可以使用.zip

    操作系统:LInux、IOS、树莓派、安卓开发、微机操作系统、网络操作系统、分布式操作系统等。此外,还有嵌入式操作系统、智能操作系统等。 网络与通信:数据传输、信号处理、网络协议、网络与通信硬件、网络安全网络...

    ios-多功能选择器,UIActionSheet,仿微信ActionSheet.zip

    在iOS开发中,UIActionSheet是一种常用的用户界面元素,它用于提供用户多个操作选项,通常在屏幕底部以半透明的视图形式呈现。这个压缩包"ios-多功能选择器,UIActionSheet,仿微信ActionSheet.zip"包含了一个自定义的...

    IOS自定义UIActionSheet的Demo

    在iOS开发中,UIActionSheet是苹果提供的一种用于展示用户可以选择的多个操作的视图,通常在用户需要执行一项或多向选择时出现。然而,原生的UIActionSheet虽然功能齐全,但有时不能满足开发者对界面个性化的需求。...

    UIActionSheet详解

    UIActionSheet是iOS应用开发中用于展示警告或提供多个选项给用户的一种UI控件。它通常在用户需要进行关键操作或者需要确认信息时出现,比如删除数据或选择操作。UIActionSheet会显示在屏幕底部,包含一个标题(可选...

    IOS应用源码——UIActionSheet 的美化效果.rar

    在iOS开发中,UIActionSheet是苹果提供的一种用于展示用户可以选择的多个操作的视图控件,通常在用户触发某种需要确认或者有多种选择的情况下出现。这个压缩包“IOS应用源码——UIActionSheet 的美化效果.rar”显然...

    UIActionSheet定制按钮的例子

    在iOS开发中,UIActionSheet是苹果提供的一个用于展示用户可以选择的多个操作的视图,通常在iPhone上作为弹出视图出现。然而,UIActionSheet的默认样式和功能有时并不能满足开发者的需求,这时就需要进行自定义。...

    ios-自定义UIActionSheet.zip

    在iOS开发中,UIActionSheet是苹果提供的一种用于展示用户可选操作的原生控件。然而,有时候系统默认的UIActionSheet样式可能无法满足所有设计需求,这时开发者就需要进行自定义来达到理想的效果。本资源"ios-自定义...

    ios的提示信息UIActionSheet和UIAlertView

    在iOS开发中,UIActionSheet和UIAlertView是两种用于向用户展示提示信息的重要控件。它们在用户交互过程中起到了至关重要的作用,特别是在需要用户做出选择或确认操作时。然而,随着iOS版本的更新,这两种控件逐渐被...

    iOS模仿系统UIActionSheet,自定义弹框

    在iOS开发中,UIActionSheet是苹果提供的一种用于展示用户可以选择的多个操作的视图,通常在用户需要进行一项或多向选择时出现。然而,原生的UIActionSheet控件存在一些限制,例如不支持自定义字体大小和颜色。鉴于...

    实现类似iOS中的UIActionSheet效果

    在iOS开发中,UIActionSheet是一种常见的用户交互控件,用于提供多个操作选项给用户,常见于底部弹出。在iOS 8之后,苹果引入了UIAlertController,它取代了UIActionSheet和UIAlertView。然而,为了在旧版本的iOS...

    UIAlertView和UIActionSheet

    UIAlertView和UIActionSheet是iOS开发中用于向用户显示警告信息和操作选项的界面组件。它们通常用于提供反馈、提示用户进行选择、警告用户操作的后果等场景。由于iOS版本的更新,某些使用方式可能会发生变化或产生...

    IOS应用源码——UIActionSheet 的美化效果.zip

    在iOS开发中,UIActionSheet是苹果提供的一个用于展示用户可以选择的不同操作的控件,它通常出现在屏幕底部,提供一种半透明的弹出视图。然而,原生的UIActionSheet设计风格可能不能满足所有应用的审美需求,因此...

Global site tag (gtag.js) - Google Analytics