UIAlertView和Actionsheet

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值