IOS学习笔记(九)之UIAlertView(警告视图)和UIActionSheet(操作表视图)基本概念和使用方法(博客地址:http://blog.csdn.net/developer_jiangqq)
Author:hmjiangqq
Email:jiangqqlmj@163.com
在平时操作中,应用需要和用户进行交流:那么会用到下面两种视图控件
(1:警告视图UIAlertView,2:操作表视图UIActionSheet)
(一):警告视图:UIAlertView
首先看下官方解说:
Use the UIAlertView
class to display an alert message to the user. An alert view functions similar to but differs in appearance from an action sheet (an instance ofUIActionSheet
).
Use the properties and methods defined in this class to set the title, message, and delegate of an alert view and configure the buttons. You must set a delegate if you add custom buttons. The delegate should conform to the UIAlertViewDelegate
protocol. Use the show
method to display an alert view once it is configured.
作用:提示用户,帮助用户和选择,用于警告或者提示。最多是两个按钮。
如果超过两个就应该使用第二种方式:操作表。
[注]在IOS开发中,警告框的权限比较高,所以我们不应该去随意的使用。一般情况下,
警告框使用的场景可以是如下的几个:
①:应用不能运行:例如:应用崩溃,或者无法获取数据,这里可以给用户一个警告。
②:询问用户: 例如:应用不能继续运行的时候,可以提示让用户去选择解决方案。
③:询问一些授权信息 :例如:我们的应用需要去访问以下用户的隐私数据,这里提示给
用户,方便用户选择。
④....等等其他场景。
UIAlertView的常用属性和方法:
实例代码如下:
-(void)showAlertView{
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"other", nil];
[alertView show];
[alertView release];
NSLog(@"showAlertView...");
}
运行截图:
同时我们可以在创建的时候设置代理(Delegate)这边用self指定,为当前控制器实现代理方法:
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"other", nil];
实现方法如下:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"点击了:%@",[alertView buttonTitleAtIndex:buttonIndex]);
}
(二):操作表视图:UIActionSheet
看下官方解说:
Use the UIActionSheet
class to present the user with a set of alternatives for how to proceed with a given task. You can also use action sheets to prompt the user to confirm a potentially dangerous action. The action sheet contains an optional title and one or more buttons, each of which corresponds to an action to take.
Use the properties and methods of this class to configure the action sheet’s message, style, and buttons before presenting it. You should also assign a delegate to your action sheet. Your delegate object is responsible for performing the action associated with any buttons when they are tapped and should conform to the UIActionSheetDelegate
protocol. For more information about implementing the methods of the delegate, see UIActionSheetDelegate Protocol Reference.
等多个平台,就应该使用操作表。操作表示是UIActionSheet创建,会从手机的屏幕底下滑出来。
UIActionSheet常用属性和方法
示例代码如下:
-(void)showActionSheet{
//操作表
UIActionSheet *actionSheet=[[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"按钮" otherButtonTitles:@"微信",@"新浪", nil]autorelease];
actionSheet.actionSheetStyle=UIActionSheetStyleBlackOpaque;
[actionSheet showInView:self.window];
NSLog(@"showActionSheet...");
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"点击了:%@",[alertView buttonTitleAtIndex:buttonIndex]);
}
[注]:UIActionSheet的actionSheetStyle属性用于设定操作表的样式,样式风格如下:
typedef NS_ENUM(NSInteger, UIActionSheetStyle) {
UIActionSheetStyleAutomatic = -1, // take appearance from toolbar style otherwise uses 'default'
UIActionSheetStyleDefault = UIBarStyleDefault, 默认样式
UIActionSheetStyleBlackTranslucent = UIBarStyleBlackTranslucent, 半透明
UIActionSheetStyleBlackOpaque = UIBarStyleBlackOpaque, 透明
};
运行截图: