天天看點

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

動作表ActionSheet和警告框AlterView是iOS中常見的元素,也是app中常見的元素,相當于一個與使用者互動的菜單。其實在《【iOS】按鈕點選彈窗》(點選打開連結)中介紹的彈窗也是警告框的一種,隻是當時以最簡單的方式呈現。iOS中的動作表和警告框與《【iOS】表視圖》(點選打開連結)同樣通過完成iOS要求的指定函數來實作。下面以一個簡單的例子,來說明這個問題。

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

如上圖所示,使用者點選按鈕将彈出動作表,之後點選選擇項之後,會彈出警告框,然後我們最終能接受到使用者點選的是哪個選擇項與哪個選擇框。以此來說明動作表和警告框的響應事件。

制作過程如下:

1、場景布置就不多說了,一個Round Rect Button、一個Label,注意Label要拖大一點以容納所有文字,當然預設的文本"Label",清楚Text屬性。

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

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]來取出,進而達到資料傳遞的效果。