【iOS】动作表ActionSheet和警告框AlterView

来源:互联网 发布:行助手检测不到网络 编辑:程序博客网 时间:2024/05/18 09:36

动作表ActionSheet和警告框AlterView是iOS中常见的元素,也是app中常见的元素,相当于一个与用户交互的菜单。其实在《【iOS】按钮点击弹窗》(点击打开链接)中介绍的弹窗也是警告框的一种,只是当时以最简单的方式呈现。iOS中的动作表和警告框与《【iOS】表视图》(点击打开链接)同样通过完成iOS要求的指定函数来实现。下面以一个简单的例子,来说明这个问题。


如上图所示,用户点击按钮将弹出动作表,之后点击选择项之后,会弹出警告框,然后我们最终能接受到用户点击的是哪个选择项与哪个选择框。以此来说明动作表和警告框的响应事件。

制作过程如下:

1、场景布置就不多说了,一个Round Rect Button、一个Label,注意Label要拖大一点以容纳所有文字,当然默认的文本"Label",清楚Text属性。


2、以《【iOS】点击按钮Button,更变标签文字Label的颜色》(点击打开链接)的方式,注册Label为label1,然后Button的点击事件为OnClick。注册之后ViewController.h如下:

////  ViewController.h//  Actionsheet////  Created by pc on 17-6-5.//  Copyright (c) 2017年 pc. All rights reserved.//#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UIAlertViewDelegate,UIActionSheetDelegate>//动作表和警告框的委托,使其响应事件生效//组件和事件的注册@property (weak, nonatomic) IBOutlet UILabel *label1;- (IBAction)OnClick:(id)sender;@end
这里同时与《【iOS】表视图》(点击打开链接)同样,添加了动作表和警告框的委托,以使其点击事件生效。

3、之后修改ViewController.m如下,这也是全文的精华。

////  ViewController.m//  Actionsheet////  Created by pc on 17-6-5.//  Copyright (c) 2017年 pc. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}//按钮点击事件- (IBAction)OnClick:(id)sender {    //初始化动作表,填写一系列基本属性    UIActionSheet *uiActionSheet = [[UIActionSheet alloc]                                    initWithTitle:nil                                    delegate:self                                    cancelButtonTitle:@"取消"                                    destructiveButtonTitle:@"警告性选择项"                                    otherButtonTitles:@"动作表选择项1",@"动作表选择项2",nil];    uiActionSheet.actionSheetStyle = UIActionSheetStyleDefault;//设置动作表样式,一般默认即可    [uiActionSheet showInView:self.view];//显示这个动作表}//动作表响应事件-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{    switch(buttonIndex){//一起按照buttonIndex来,从上到下的按钮的点击事件依次为0-n        case 0:            [self showAlert:@"警告性选择项"];            break;        case 1:            [self showAlert:@"动作表选择项1"];            break;        case 2:            [self showAlert:@"动作表选择项2"];            break;    }}//自定义的showAlert函数,同时带有msg的形式参数-(void)showAlert:(NSString *)msg {    //初始化警告框,填写一系列基本属性    UIAlertView *alert = [[UIAlertView alloc]                          initWithTitle:@"你选择的动作表选择项为:"                          message:msg                          delegate:self                          cancelButtonTitle:nil                          otherButtonTitles:@"弹窗按钮1",@"弹窗按钮2",nil];    [alert setMessage:[[NSString alloc]initWithFormat:@"%@\n",msg]];//设置警告框中的message属性,专门用来和响应事件alertView作数据传递用的    [alert show];//弹窗    }//警告框的响应事件- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger) buttonIndex{    self.label1.numberOfLines = 0;//让\n生效,iOS默认\n换行是不生效的,我也是醉了    self.label1.text=[[NSString alloc]initWithFormat:@"%@%@%@%d",                      @"你选择的为:\n",[alertView message],@"弹窗按钮",(buttonIndex+1)];    }@end
需要说明的是,在ViewController.h添加的委托<UIAlertViewDelegate,UIActionSheetDelegate>对应的函数是-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex和- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger) buttonIndex必须要完成,这里大家可能有困惑,一个动作表的选项对应一个警告框,那么警告框有多个,如何识别呢?这里就是通过[alert setMessage:[]]用于识别不同的警告框,如果你要传的是int那就用[alert setTags:[]]。之后可以用[alertView Message]或者[alertView Tags]来取出,从而达到数据传递的效果。

阅读全文
0 0
原创粉丝点击