IOS开发(七):UIActionSheet和UIAlertView

UIAlertView类可以创建一个简单的模态提醒窗口,其中包含一条消息和几个按钮,还可能有普通文本框和密码文本框等。 

一般要求用户必须与之交互(如按下按钮)后才能做其他事情,它们通常位于其它窗口前面,在可见时禁止用户与其它任何界面元素交互。

一、UIAlertView基础

要实现提醒视图,首先需要声明一个UIAlertView对象,再初始化并显示它。最简单的情形如下所示:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        messgae:@"这是一个简单的警告框"
                                                        delegate:nil
                                                        cancelButtonTitle:@"好"
                                                        otherButtonTitles:nil];

[alert show];
[alert release];

此外,可以给它添加多个按钮,比如:otherButtonTitles:@"按钮1",@"按钮2",@"按钮3",nil,这样就可以创建多个按钮。


响应问题:

对于多个按钮的情形,怎样知道用户点击的是哪一个按钮呢?在UIAlertView中有一个委托UIAlertViewDelegate,通过继承该委托的方法可以实现点击事件处理。

二、UIActionSheet

UIActionSheet从底部冒出一个框,一般有2个或更多的选项让用户点击。

------------------------------------------------------------------------------------------

来自网络的资料:http://www.cnblogs.com/minglz/category/424628.html

ios中有很多已经定义好的类可以供我们在编写程序时直接使用,例如UIActionSheet、UIAlertView等,这些类定义了很多method,我们可以调用这些method且不必知道这些method是如何实现的。
但是有一个问题,如果我们想改变这些method的实现,那我们该这么做呢?
一种方法是继承,我们可以继承一个类,然后在自己的类中重新写method,这是一个方法,但不是一个很方便的方法,有时候你仅仅需要改变很小的一个功能,却要继承一个很大的类,貌似有点复杂了,而且如果你需要一些不同的实现,那你就需要定义好多不同的类,这会很麻烦。
为了使开发过程更加的方便,ios使用了另一种方法来达到同样的目的,就是使用delegate,我们使用一个已定义的类,然后使用委托\代理来改写类中的method,程序在运行时,delegate发现你创建了某个类的实例且改写了其中的method,这样程序在运行时就不会去调用原有的实现(当然你也可以调用原有的实现),而是直接调用你写的新的实现,从而达到自定义程序方法的目的。

1、使用UIActionSheet

项目简介:点击按钮,弹出一个UIActionSheet,比如在buttonPressed方法中实现:设置Button的Action,名字为buttonPressed。

    1)、添加<UIActionSheetDelegate>
我们需要在BIDViewController类中使用UIActionSheet,而使用UIActionSheet时我们需要实现其一个delegate(并不是所有的delegate方法都要实现,只要根据实际需求去实现某些method,这个例子中的UIAlertView就不需要实现任何的delegate),但是在BIDViewController类中并没有这个delegate的实现,因此需要手动添加,而在BIDViewController.h中添加<UIActionSheetDelegate>,就是让BIDViewController可以接收并响应UIActionSheet的代理事件。
打开BIDViewController.h,添加<UIActionSheetDelegate>:

@interface ViewController : UIViewController<UIActionSheetDelegate>

    2)、实现buttonPressed方法

//按钮按下去的事件
- (IBAction)buttonPressed:(id)sender {
    UIActionSheet * actionSheet = [[UIActionSheet alloc]
                                   initWithTitle:@"Are you sure?"
                                   delegate:self
                                   cancelButtonTitle:@"No Way!"
                                   destructiveButtonTitle:@"Yes, I'm Sure!"
                                   otherButtonTitles:nil];
//设置操作表的样式
    actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;

    [actionSheet showInView:self.view];
}

注:
如果有工具栏或者选项卡栏,可以使用showFromToolBar:或者showFromTabBar:方法,让操作表看起来是从这些用户界面元素中打开的。这说明操作表可以和给定的视图或者其它关联起来,而警告框则不能。


    3)、分析ActionSheet的代码

initWithTitle:@"Are you sure":ActionSheet的title

delegate:self指明这个UIActionSheet的代理在哪里,self说明这个代理在本类中,也就是说在UIActionSheet所在的类中寻找UIActionSheet的代理方法的实现(这个例子中的类就是指类BIDViewController)。回过头再去看BIDViewController.h中我们刚刚添加的<UIActionSheetDelegate>,让该类可以接收并响应UIActionSheet的代理事件。

cancelButtonTitle:@"No Way!":取消按钮,用于取消(不继续进行下一步操作),这里设置取消按钮的文字。

destructiveButtonTitle:@"Yes, I'm Sure!":相当于确定按钮(继续下一步操作),这里设置确定按钮的文字。

otherButtonTitles:nil:除了上面的取消按钮和确定按钮外,ActionSheet还可以自定义多个按钮,这里设置其他按钮的文字(例如:otherButtonTitles:@"Foo", @"Bar", nil;最后一个参数一定要写nil,表示结束)。

上面code中的最后一行:
[actionSheet showInView:self.view]
作用是显示actionSheet,每一个ActionSheet都需要有一个parent view,在parent view中显示自己,因为我们是单一视图项目(Single View),也只有一个View,因此这里的self.view就是说在actionSheet实现的这个view里显示。

    4)、实现actionSheet delegate方法

        负责响应actionSheet的类必须遵守协议UIActionSheetDelegate;

        必须将actionSheet的属性delegate设置为实现了该协议的对象,如果负责响应和调用actionSheet的是同一个对象,则只需将actionSheet设置为self即可。


在BIDViewController.m中的buttonPressed方法下面添加另一个方法,如下code:

 

//actionSheet的委托方法
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if(buttonIndex != [actionSheet cancelButtonIndex])
    {
        NSString *msg = nil;
        
        if(nameField.text.length > 0){
        
            msg = [[NSString alloc] initWithFormat:@"You can breathe easy, %@, everything went OK.", nameField.text];
        }
        else{
            msg = @"NO CONTENT!";
        }
        
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Something was done"
                              message:msg
                              delegate:self
                              cancelButtonTitle:@"Phew!"
                              otherButtonTitles:nil];
        
        [alert show];
    }
}

上面的code实现了一个Action Sheet的delegate:didDismissWithButtonIndex,当点击actionSheet按钮时,会调用到该delegate,而不会去调用其原有的方法。在该方法中,首先判断用户没有点击cancelButton(根据button的Index来判断),如果确实没有点击cancelbutton(点击了destructiveButton,因为只有2个button),就显示一个警告框。

UIAlertView的参数说明:
initWithTitle:@"Something was done":Alert的title

message:msg:Alert的文字

delegate:self:作用和ActionSheet中的类似,只是UIAlertView没有实现任何delegate方法,因此我们也没有在头文件中引入<UIAlertViewDelegate>

cancelButtonTitle:@"Phew!":取消按钮,可以看作是关闭Alert窗口的按钮,然后什么操作都不继续。

otherButtonTitles:nil:作用和ActionSheet中的一样

总的来看,UIAlertView和UIActionSheet的实现相当类似,可以对比着进行学习。


自定义动画效果的UIAlertView:

 

UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"请等待"
                              message:nil
                              delegate:nil
                              cancelButtonTitle:nil
                              otherButtonTitles:nil];
        
        [alert show];
        
        UIActivityIndicatorView *activeView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        activeView.center = CGPointMake(alert.bounds.size.width / 2.0f, alert.bounds.size.height - 40.0f);
        [activeView startAnimating];
        [alert addSubview:activeView];

附:ActionSheet的响应点击选项的方法

 

//重写父类的方法:相应的选项被点击后的处理
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if(buttonIndex == 0){
        [self showAlertWithMsg:@"点击了确定"];
    }else if (buttonIndex == 1){
        [self showAlertWithMsg:@"点击了第一项"];
    }
    
}

 

//封装调用警告框的方法
-(void) showAlertWithMsg:(NSString *)msg{
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"操作表"
                          message:msg
                          delegate:self
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil];
    [alert show];
}

 

posted @ 2013-04-10 16:35  XuShao  阅读(343)  评论(0编辑  收藏  举报