警示框

来源:互联网 发布:北京哪里体检最好 知乎 编辑:程序博客网 时间:2024/04/27 14:35

   1、UIActionSheet创建一个简单动作表,

 UIActionSheet *actionSheet = [[UIActionSheetalloc]initWithTitle:@"Choose action"

                                                             delegate:nilcancelButtonTitle:nildestructiveButtonTitle:nilotherButtonTitles:nil];//基本的初始化、代理设置、按钮的设置

//初始化的时候也可以增加多个按钮

 UIActionSheet *actionSheet = [[UIActionSheet allocinitWithTitle:@"Choose action"

                                                              delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@“1”,@"2",@“3”,@"4"];

//基本的初始化、代理设置、按钮的设置


2、增加按钮的方法

    [actionSheet addButtonWithTitle:@"Take picture"];

    [actionSheet addButtonWithTitle:@"Choose picture"];

    [actionSheet addButtonWithTitle:@"Take video"];

    [actionSheet addButtonWithTitle:@"Cancel"];//增加按钮

    //set the desctructiveButtonIndex to the button that is responsible for the cancel function

   

3、设置按钮索引的数字

 actionSheet.destructiveButtonIndex=3;//

[actionSheet showInView:self.view];// 注意这里显示方法和alertView的区别


4、显示ActtionSheet的方法

- (void)showFromToolbar:(UIToolbar *)view;

- (void)showFromTabBar:(UITabBar *)view

- (void)showFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animatedNS_AVAILABLE_IOS(3_2);

- (void)showFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animatedNS_AVAILABLE_IOS(3_2);//在指定的区域内显示

//会有这些方法就是因为显示的时候并不一定要选择,而下面的方法一定要选择一个,因为他是在view界面,会成为第一xia响应者,而上面的不会成为第一响应者。

- (void)showInView:(UIView *)view;


@interface YDViewController ()<UIActionSheetDelegate>

点击了之后调用的代理方法

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

{

    int index = buttonIndex;

   

    [actionSheet dismissWithClickedButtonIndex:buttonIndexanimated:YES];//这个方法调用就会解散这个actionSheet,并且在索引上使用开关来识别用户做何种选择。

//不过经过测试,好像这句话没有起到作用?????

    switch (index) {

        case0:

        {

        NSLog(@"Take picture selected");

        }

            break;

        case1:

        {

        NSLog(@"Choose picture selected");

        }

            break;

        case2:

        {

        NSLog(@"Take video selected");

        }

            break;

        case3:

        {

        NSLog(@"Cancel selected");

        }

            break;

        default:

            break;

    }

}

上面的方法就是点击上面的按钮之后就会调用到这个方法,通过按钮的索引来区别不同的按钮。



UIalertView这里的UIalertView是通过相关的alert样式来显示。

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

{

    switch (buttonIndex) {

        case0:

        {

            //NO Selection or single button action

        }

            break;

        case1:

        {//OK

 

        }

            break;

        default:

            break;

    }

    

    //to access the UITextFields you can use

    if (alertView.alertViewStyle ==UIAlertViewStylePlainTextInput)

        {

          UITextField* username = [alertViewtextFieldAtIndex:0];

          //use the username instance

        }

    elseif (alertView.alertViewStyle ==UIAlertViewStyleSecureTextInput)

        {

        UITextField* password = [alertViewtextFieldAtIndex:0];

        //use the password instance

        }

    else if (alertView.alertViewStyle ==UIAlertViewStyleLoginAndPasswordInput)

        {

        

        UITextField* username = [alertViewtextFieldAtIndex:0];

        UITextField* password = [alertViewtextFieldAtIndex:1];

        //use the username and password instance

        }


}



0 0
原创粉丝点击