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