您的位置:首页 > 产品设计 > UI/UE

iOS控件之UIAlertView

2016-06-27 20:26 375 查看
UIAlertView就是我们常说的警告视图 

作用:提示用户,帮助用户选择

在IOS中主要有2种形式   1.是alert警告 弹出带有震动效果 主要是给用户一个通知

                    2.是ActionSheet 会在屏幕底部滑出 相当于产生一个占屏幕1/3大小的view

                         可以通过该窗口将信息发布到如 微薄  人人等资源上

ActionSheet和AlertView 比较相似都是给用户一个提示信息。它是从底部弹出。它通常用于确认潜 

在的危险或不能撤消的操作,如删除一个数据。  

iOS程序中的Action Sheet就像Windows中的 “确定-取消”对话框一样,用于强制用户进行选择。当用户将要进行的操作具有一定危险时,常常使用Action Sheet对用户进行危险提示,这样,用户有机会进行取消操作。

 

Action Sheet需要多个参数:

(1)initWithTitle:设置标题,将会显示在Action Sheet的顶部

(2)delegate:设置Action Sheet的委托。当Action Sheet的一个按钮被按下后,它的delegate将会被通知,并且会执行这个delegate的actionSheet: didDismissWithButtonIndex方法将会执行。这里,我们将delegate设成self,这样可以保证执行我们自己在ViewController.m写的actionSheet: didDismissWithButtonIndex方法

(3)cancelButtonTitle:设置取消按钮的标题,这个取消按钮将会显示在Action Sheet的最下边

(4)destructiveButtonTitle:设置第一个确定按钮的标题,这个按钮可以理解成:"好的,继续"

(5)otherButtonTitles:可以设置任意多的确定按钮

Alert相当于Windows中的Messagebox,跟Action Sheet也是类似的。不同的是,Alert可以只有一个选择项,而Action Sheet却至少要两个选项。

Alert也要填写很多参数:

 

(1)initWithTitle:设置标题,将会显示在Alert的顶部

(2)message:设置提示消息内容

(3)delegate:设置Alert的委托。这里,我们设成self

(4)cancelButtonTitle:设置取消按钮的标题

(5)otherButtonTitles:与Action Sheet类似

[alert show]这条语句用来显示Alert。

project: UIAlertOrSheetDemo  

new file ...

              name:AlertView

              superclass:UIAlertView

打开  AlertView.m

加入

- (void)dealloc

{

//    NSLog(@"dead : %d", self.tag);

    [super dealloc];

}

打开 AppDelegate.m

加入

#import "AlertView.h"

在  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

中的    [self.window makeKeyAndVisible];  顶上加入

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    button1.frame = CGRectMake(10, 100, 140, 40);

    [button1 setTitle:@"alertView" forState:UIControlStateNormal];

    [button1 addTarget:self action:@selector(showAlertView) forControlEvents:UIControlEventTouchUpInside];

    [self.window addSubview:button1];

    

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    button2.frame = CGRectMake(240-70, 100, 140, 40);

    [button2 setTitle:@"actionSheet" forState:UIControlStateNormal];

    [button2 addTarget:self action:@selector(showActionView) forControlEvents:UIControlEventTouchUpInside];

    [self.window addSubview:button2];

在加入方法

- (void)showAlertView

{                  //第一个是取消按钮 之后是确定按钮

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"other1", @"other2", nil] ;

    [alertView show];

    

}

- (void)showActionView

{

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"title" delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:@"destructive" otherButtonTitles:@"other1", @"other2", @"other3", @"other3", nil] ;

    [actionSheet showInView:self.window];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息