警告框和操作表(ActionSheet)

来源:互联网 发布:linux 查看cpu日志 编辑:程序博客网 时间:2024/06/05 01:02

iOS 开发中,应用程序如何实现与用户的交流呢?警告框和操作表就是为此设计的。

警告框时UIAlertView创建的,用于为用户以警告或提示,最多有两个按钮,超过两个就应该考虑使用操作表。由于在iOS中,警告框是”模态“(表示的是不关闭它就不能做别的事情),因此不应该随意使用,一般情况下,警告框的使用场景有如下几个:

-应用不能继续进行。例如,无法获得网络数据或者功能不能完成的时候,给用户一个警告,这种警告框只需一个按钮。

-询问另外的解决方案。好多应用在不能继续运行时,会给出另外的解决方案,让用户去选择。例如,WiFi网络无法连接时,是否使用3G网络。

-询问对操作的授权。当应用访问用户的一些隐私信息时,需要用户授权,例如用户当前的位置;通讯录或者日程表等。


AlertView的用法注意:

方法中实例化UIAlertView的对象时,最常用的构造函数是initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:,其中delegate参数场设置为self,既警告框的委托对象为当前的视图控制器(ViewController);cancelButtonTitle参数用于设置“取消”按钮的标题,它是警告框的左按钮;otherButtonTitles参数是其他按钮,它是一个字符串数组,该字符串数组以nil结尾。

从用户体验上讲,警告框的按钮不能多于两个。

当用户警告框只有一个按钮时,只是为了关闭警告框,这时不需要指定委托参数。但是有两个按钮的情况下,为了响应点击警告框的需要,常需要设置委托。


ActionSheet的用法注意:

如果想给用户提供多于两个的选择,

如上图所示,就应该使用操作表。

操作表是UIActionSheet创建的,在iPhone下运行会从屏幕下方滑出来,其布局是最下面是一个“取消”按钮“,它离用户的大拇指最近,最容易点到。如果选中有一个破坏性的操作,将会放在最上面,是大拇指最不容易碰到的位置,并且其颜色是红色的。

注意在iPad中,操作表的布局与iPhone有所不同,在iPad中,操作表不是从底部滑出来的,而是随机出现在触发它的按钮周围。此外,它还没有”取消“按钮,即便是在程序中定义了”取消“按钮,也不会显示它。

在实例化UIActionSheet对象时,最常用的构造函数是initWithTitle:delegate:cancelButtonTitle:destructiveButtonTitle:otherButtonTitles:。

UIActionSheet的actionSheetStyle属性德用于设定操作表的样式:

UIActionSheetStyleAutomatic。自动样式。

UIActionSheetStyleDefault。默认样式。

UIActionSheetStyleBlackTranslucent。半透明样式。

UIActionSheetStyleBlackOpaque。透明样式。


本次实例:

//

//  ViewController.h

//  1006AlertViewAndActionSheet(操作表)

//

//  Created by weibiao on 15-10-6.

//  Copyright (c) 2015 weibiao. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface ViewController : UIViewController


- (IBAction)testAlertView:(id)sender;

- (IBAction)testActionSheet:(id)sender;


@end


//

//  ViewController.m

//  1006AlertViewAndActionSheet(操作表)

//

//  Created by weibiao on 15-10-6.

//  Copyright (c) 2015 weibiao. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()<UIAlertViewDelegate,UIActionSheetDelegate>


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (IBAction)testAlertView:(id)sender {

    UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"Alert"message:@"Alert text goes here"delegate:selfcancelButtonTitle:@"NO"otherButtonTitles:@"YES",nil];

    [alert show];

    NSLog(@"test alert");

}


- (IBAction)testActionSheet:(id)sender {

    UIActionSheet *actionSheet = [[UIActionSheetalloc] initWithTitle:@"ActionSheet"delegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:@"毁坏性按钮"otherButtonTitles:@"FaceBook",@"新浪微博",nil];

    actionSheet.actionSheetStyle =UIActionSheetStyleAutomatic;

    [actionSheet showInView:self.view];

}

@end



0 0
原创粉丝点击