iphone开发 UIActionSheet(操作表) 和UIAlertView(警告)的用法

来源:互联网 发布:同轴电缆传输网络结构 编辑:程序博客网 时间:2024/04/30 09:41

一.UIActionSheet(操作表) 和UIAlertView(警告)

 

UIActionSheet用于迫使用户在两个或更多的选项之间进行选择的模式视图。操作表从屏幕底部弹出,显示一系列按钮供用户选择,用户只有单击一个按钮后才能继续使用应用程序。

UIAlertView警告以蓝色圆角矩形形式出现在屏幕中部,警报可显示一个或多个按钮

为了让控制器类充当操作表的委托,控制器需要遵从UIActionSheetDelegate协议

 

二.UIActionSheet(操作表)的创建

 

initWithTitle:delegate:cancelButtonTitle:destructiveButtonTitle:otherButtonTitles:

 

注释:initWithTitle是对象函数

            initWithTitle:操作表标题

            delegate:接收对象

     cancelButtonTitle关闭按钮标题

            destructiveButtonTitle: 操作按钮标题

            otherButtonTitles:其他按钮

 

1.   创建一个简单的操作表:

    //open a dialog with an OK and cancel button

    UIActionSheet*actionSheet = [[UIActionSheet alloc]  

                  initWithTitle:@" UIActionSheet<title>" 

                   delegate:self   

                  cancelButtonTitle:@"Cancel"

                  destructiveButtonTitle:@"OK"

                    otherButtonTitles:nil];

 

    actionSheet.actionSheetStyle = UIActionSheetStyleDefault;

 

    [actionSheetshowInView:self.view]; //show from our table view (pops up in the middle of the table)

  

    [actionSheetrelease];



 

2.    创建两个按钮的操作表

            // open a dialog with an OKand cancel button

    UIActionSheet*actionSheet = [[UIActionSheet alloc]

                   initWithTitle:@" UIActionSheet<title>"

                    delegate:self

                   cancelButtonTitle:@"Cancel" 

                    destructiveButtonTitle:@"OK" 

                   otherButtonTitles:nil];

 

    actionSheet.actionSheetStyle = UIActionSheetStyleDefault;

    [actionSheetshowInView:self.view]; //show from our table view (pops up in the middle of the table)

    [actionSheetrelease];

 

3.   创建自定义操作表:

 

    //open a dialog with two custom buttons

    UIActionSheet*actionSheet = [[UIActionSheet alloc]

                   initWithTitle:@" UIActionSheet<title>"

                    delegate:self

                   cancelButtonTitle:nil

                    destructiveButtonTitle:nil

                otherButtonTitles:@"Button1", @"Button2", nil];

 

    actionSheet.actionSheetStyle = UIActionSheetStyleDefault;

 

    actionSheet.destructiveButtonIndex = 1; // make the second button red(destructive)

 

    [actionSheetshowInView:self.view]; //show from our table view (pops up in the middle of the table)

 

   [actionSheet release];


对操作表中按钮的操作用UIActionSheetDelegate协议函数,这里你可以控制在点击按钮之后的操作

#pragma mark - UIActionSheetDelegate

 

- (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

    //the user clicked one of the OK/Cancel buttons

    if(buttonIndex == 0)

    {

        //NSLog(@"ok");

    }

    else

    {

        //NSLog(@"cancel");

    }

}

三.UIAlertView(警告)的创建

 

      initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:

   注释:initWithTitle是对象函数

               initWithTitle:警告框标题

               message:警告内容

               delegate:接收对象

               cancelButtonTitle:关闭按钮标题

               otherButtonTitles:其他按钮

 

1.    创建一个简单的警告框

 

    //open an alert with just an OK button

    UIAlertView*alert = [[UIAlertView alloc]

                           initWithTitle:@"UIAlertView"

                          message:@"<Alertmessage>"

                        delegate:self

                           cancelButtonTitle:@"OK"

                           otherButtonTitles: nil];

 

    [alert show];//显示警告框

    [alert release];//释放警告框


 

2.    创建两个按钮的警告框

     // open a alert with an OK andcancel button

    UIAlertView*alert = [[UIAlertView alloc] 

                           initWithTitle:@"UIAlertView"

                           message:@"<Alertmessage>"

                        delegate:self

                           cancelButtonTitle:@"Cancel"

                           otherButtonTitles:@"OK", nil];

 

    [alert show];//显示警告框

    [alert release];//释放警告框


3.  创建自定义警告框

    //open an alert with two custom buttons

    UIAlertView*alert = [[UIAlertView alloc]

                           initWithTitle:@"UIAlertView"

                           message:@"<Alertmessage>"

                        delegate:self

                           cancelButtonTitle:@"Cancel"

                otherButtonTitles:@"Button1",@"Button2", nil];

 

       

[alert show];//显示警告框

[alert release];//释放警告框

 

 

4   警告框中按钮的操作

对警告框中按钮的操作用UIAlertViewDelegate协议函数,这里你可以控制在点击按钮之后的操作

#pragma mark - UIAlertViewDelegate

 

- (void)alertView:(UIAlertView*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

    //use "buttonIndex" to decide your action

    //

}

 

5   给警告框添加子视图:

 

    UIAlertView*alert = [[UIAlertView alloc]     initWithTitle:@"UIAlertView " 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]; 

   [activeView release]; 

    [alert release];

 

 

 

 

 

 

 

原创粉丝点击