UIAlertView和Actionsheet
来源:互联网 发布:移动数据漫游什么意思 编辑:程序博客网 时间:2024/06/06 18:30
UIAlertView和Actionsheet都可以用来向用户反馈消息。
不同的是,UIAlertView一般只是简单地向用户展示一些提示消息或警告消息。
而当需要根据用户的选择作出不同的响应的时候,我们还是使用actionsheet比较多。虽然UIAlertView也可以做得到。
创建UIAlertView:
UIAlertView *messagebox = [[UIAlertView alloc] initWithTitle:@"notice" message:@"this is an alert view" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"OK", nil]; [messagebox show];
以上代码就创建一个简单的警告窗口,使用该实例的show方法显示窗口。
运行结果:
一般我们无须自己编写代码处理用户的行为,用户只需简单点击一下按钮警告框就会消失。
值得注意的是:
UIAlertView警告框并不是阻塞式的,也就是说,当这个警告窗口显示出来的时候,[messagebox show]后面的代码会继续运行。
用户点击了哪个按钮:实现UIAlertViewDelegate协议
当我们需要知道用户点击了哪一个按钮,我们就要实现UIAlertViewDelegate协议里面的alertView方法
ViewController.h:
@interface ViewController : UIViewController <UIAlertViewDelegate>{ IBOutlet UILabel *label;}@property UILabel *label;-(IBAction)showMessage:(id)sender;@end
ViewController.m
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize label;- (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)showMessage:(id)sender{ UIAlertView *messagebox = [[UIAlertView alloc] initWithTitle:@"notice" message:@"this is an alert view" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"OK", nil]; [messagebox show]; [label setText:@"label was changed"];}-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSLog(@"%d", buttonIndex); //在控制台显示buttonIndex的值}@end
上面代码中的alertView方法(在协议UIAlertViewDelegate里面)就是用来获取按钮的索引的,按钮的索引是一个整型的变量buttonIndex
获取到这个变量的值,我们就知道用户点击了哪个按钮。
注意:当我们使用delegate协议的时候,initWithTitle方法的delegate参数一定不能是nil,否则后面的alertview方法就无法获取按钮的索引。
创建ActionSheet:
ActionSheet的使用基本上和AlertView差不多,要获取按钮的buttonIndex,要实现UIActionSheetDelegate里面的actionSheet方法。
运行结果:
下面是完整的代码
ViewController.h
#import <UIKit/UIKit.h>@interface ViewController : UIViewController <UIAlertViewDelegate, UIActionSheetDelegate>{ IBOutlet UILabel *label;}@property UILabel *label;-(IBAction)showMessage:(id)sender;-(IBAction) showActionsheet:(id)sender;@end
ViewController.m
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize label;- (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)showMessage:(id)sender{ UIAlertView *messagebox = [[UIAlertView alloc] initWithTitle:@"notice" message:@"this is an alert view" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"OK", nil]; [messagebox show]; [label setText:@"label was changed"];}-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSLog(@"%d", buttonIndex);}-(IBAction) showActionsheet:(id)sender{ UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:@"what do you want" delegate:self cancelButtonTitle:@"OK" destructiveButtonTitle:@"delete message" otherButtonTitles:@"save", nil]; [actionsheet showInView:self.view];}-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ NSLog(@"%d", buttonIndex);}@end
0 0
- UIAlertView和Actionsheet
- IOS学习 UIAlertView and ActionSheet
- 通过UIAlertView或者ActionSheet控件调用share方法
- ActionSheet
- ActionSheet
- IOS8 alertView 和 actionSheet 使用方法
- Actionsheet和AlertView的使用
- 自定义actionSheet 字体和颜色
- ActionSheet和AlertView的封装
- 代码实现ActionSheet 和 Alert 基本功能
- ios8 下面的alert和actionsheet
- 操作表ActionSheet和警报AlertView
- IOS8 AlertView和ActionSheet不兼容问题解决方案
- 警告框和操作表(ActionSheet)
- actionSheet和从相册选取图片
- iOS8.0前后关于alertView和actionSheet
- IOS-UIAlertView和UIActionSheet
- UIActionSheet和UIAlertView
- PAT basic1006-1010(Python版)
- MD5算法---java源代码
- opengl(mesa) for ubuntu14.04
- struts2中的结果视图类型
- mysql相关(linux)
- UIAlertView和Actionsheet
- syntax error : missing ';' before '*' 'CMyImageProcessOneDoc' : missing storage-class or type 'CM
- cmake 学习之路 记录中间所遇到过的所有错误与问题的解决 <一>
- JetBrain WebStorm 9 注册码
- 第三种黑猩猩 人类的身世与未来 1
- PAT basic1011-1015(Python版)
- 初识JVM中堆栈的存储
- Fragment与FragmentActivity的关系
- MongoDB单例及简易集群安装(linux)