首页 > 其他 > 详细

创建模态提醒窗口(UIAlertView)

时间:2014-03-13 03:06:39      阅读:556      评论:0      收藏:0      [点我收藏+]

UIAlertView类创建一个简单的模态提醒窗口,可能包含消息、按钮以及文本框。模态UI元素要求用户必须与之交互(通常是按下按钮)后才能做其它事情。它们通常位于其他窗口前面,在可见时禁止用户与其他任何界面元素交互。

UIAlertView的初始化代码如下:

bubuko.com,布布扣
- (void)testAlert
{
    UIAlertView *alertView = [[UIAlertView alloc] 
                              initWithTitle:@"系统警告" 
                              message:@"您的电量不足,将在10分钟后关机"
                              delegate:nil
                              cancelButtonTitle:@""
                              otherButtonTitles:nil];

    alertView.alertViewStyle = UIAlertViewStyleDefault;
    [alertView show];
}
bubuko.com,布布扣

调用该方法后,会产生如下界面:

bubuko.com,布布扣

参数说明:
initWithTitle --- 初始化提醒视图并设置出现在提醒视图顶端的标题。
message --- 详细文本描述
delegate --- 用来响应提醒的对象。如果不需要执行任何操作,可设置为nil
cancelButtonTitle --- 视图中默认按钮的标题
otherButtonTitles --- 视图中额外按钮的标题,是一个数组,以nil结尾

alertView.alertViewStyle用来定义提醒视图的外观,有4种方案可供选择:
UIAlertViewStyleDefault --- 没有设置样式时默认采用的样式,不包含输入文本框
UIAlertViewStylePlainTextInput --- 添加一个常规输入文本框
UIAlertViewStyleSecureTextInput --- 添加一个安全(密码)文本框
UIAlertViewStyleLoginAndPasswordInput --- 添加一个常规文本框和密码文本框

需要着重了解的是delegate这个参数,它指明了由哪个类来响应用户的操作或读取文本框的内容。响应类必须遵守协议UIAlertViewDelegate并实现方法alertView:clickedButtonAtIndex,一般会指定为self。

首先在类的头文件(.h)里声明为遵守UIAlertViewDelegate协议

bubuko.com,布布扣
@interface ViewController : UIViewController <UIAlertViewDelegate>
bubuko.com,布布扣

然后实现方法alertView:clickedButtonAtIndex

bubuko.com,布布扣
- (IBAction)testUIAlertView:(id)sender 
{
    UIAlertView *alertView = [[UIAlertView alloc] 
                             initWithTitle:@"新版本提示" 
                             message:@"该程序有一个新版本,是否升级?" 
                             delegate:self 
                             cancelButtonTitle:@"忽略" 
                             otherButtonTitles:@"现在升级", nil];
    [alertView show];
}
                              
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"点击按钮的索引是:%d,标题是:%@",buttonIndex,[alertView buttonTitleAtIndex:buttonIndex]);
    if([alertView.title isEqualToString:@"Input your Email"])
    {
        NSLog(@"这个窗口是Email输入提示!");
    }
}
bubuko.com,布布扣

buttonTitleAtIndex方法是根据按钮的索引来取得按钮的标题。假如提醒框里含有输入框,则可以用textFieldAtIndex来获得该TextField实例,例如:

bubuko.com,布布扣
[[alertView textFieldAtIndex:0] text];
bubuko.com,布布扣

也可以自定义一个类来作为UIAlertView实例的delegate,一样的先声明为遵守UIAlertViewDelegate协议,然后将delegate设置为新建类的实例就可以了。但有一点一定要注意,在启用ARC后,实例变量的生存周期只存在于所属方法内部,这里是一个按钮的点击动作,当点击按钮弹出提醒框后,该代理类的引用已经被自动清除掉了,所以当你视图去点击提醒框按钮来触发alertView:clickedButtonAtIndex方法,会出现错误。这里最简单的解决方法是将变量声明为全局变量。下面的例子演示了这个问题:

bubuko.com,布布扣
MyDelegate *delegate;

- (IBAction)testUIAlertView:(id)sender 
{
    //MyDelegate *delegate = [MyDelegate alloc]; //注意,这样写有问题!
    
    delegate = [MyDelegate alloc];
    
    UIAlertView *alertView = [[UIAlertView alloc] 
                             initWithTitle:@"新版本提示" 
                             message:@"该程序有一个新版本,是否升级?" 
                             delegate:delegate 
                             cancelButtonTitle:@"忽略" 
                             otherButtonTitles:@"现在升级", nil];
    [alertView show];
}
bubuko.com,布布扣

创建模态提醒窗口(UIAlertView),布布扣,bubuko.com

创建模态提醒窗口(UIAlertView)

原文:http://www.cnblogs.com/wayne23/p/3596734.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!