如何在目标c中的UIAlertView上显示UIActionSheet

时间:2017-01-30 23:17:28

标签: ios objective-c uialertview uiactionsheet

我想在UIAlertView上显示UIActionSheet。但是根据下面的代码,它将被UIAlertView隐藏。 (见下面的截图) enter image description here

首先我展示了自定义UIAlertView,当用户点击Units时,显示UIActionSheet。但它隐藏在UIAlertView之外。下面是我的代码集。

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

    if (actionSheet.tag == 2) {
        return;
    }

    if (actionSheet.tag == 1 && (int)buttonIndex == 4) {

        UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 310, 60)]; 

        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(5,0,100,25)];
        label1.text = @"Dimensions:";
        [v addSubview:label1];

        UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(100,0,60,25)];
        textField1.borderStyle = UITextBorderStyleRoundedRect;
        textField1.placeholder = @"Width";
        textField1.keyboardAppearance = UIKeyboardAppearanceAlert;
        [textField1 setValue:[UIFont fontWithName: @"San Francisco" size:12] forKeyPath:@"_placeholderLabel.font"];
        textField1.delegate = self;
        [v addSubview:textField1];

        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(172,0,25,25)];
        label.text = @"X";
        [v addSubview:label];


        UITextField *textField3 = [[UITextField alloc] initWithFrame:CGRectMake(195,0,60,25)];
        textField3.placeholder = @"Height";
        textField3.borderStyle = UITextBorderStyleRoundedRect;
        textField3.keyboardAppearance = UIKeyboardAppearanceAlert;
        [textField3 setValue:[UIFont fontWithName: @"San Francisco" size:12] forKeyPath:@"_placeholderLabel.font"];
        textField3.delegate = self;
        [v addSubview:textField3];


        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(5,30,50,25)];
        label2.text = @"Units:";
        [v addSubview:label2];

        UITextField *textField4 = [[ReadOnlyTextField alloc] initWithFrame:CGRectMake(100,30,155,25)]; //145
        textField4.placeholder = @"-- Select an Unit --";
        textField4.borderStyle = UITextBorderStyleRoundedRect;
        textField4.keyboardAppearance = UIKeyboardAppearanceAlert;
        [textField4 setValue:[UIFont fontWithName: @"San Francisco" size:12] forKeyPath:@"_placeholderLabel.font"];

        [textField4 addTarget:self action:@selector(selectUnit:) forControlEvents:UIControlEventTouchDown];

        textField4.delegate = self;
        [v addSubview:textField4];


        av = [[UIAlertView alloc] initWithTitle:@"Dimensions" message:@"" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Download", nil];
        [av setValue:v  forKey:@"accessoryView"];
        [av show];
    }

-(void)selectUnit:(id)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select an option" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

    actionSheet.tag = 2;

    [actionSheet addButtonWithTitle:@"Pixels"];
    [actionSheet addButtonWithTitle:@"Inches"];
    [actionSheet addButtonWithTitle:@"Centimetres"];
    [actionSheet addButtonWithTitle:@"Millimetres"];
    [actionSheet addButtonWithTitle:@"Cancel"];

    [actionSheet showInView:self.view]; //[UIApplication sharedApplication].keyWindow.rootViewController.view

}

那么如何在自定义的UIAlertView ????

之上显示UIACtionSheet

1 个答案:

答案 0 :(得分:0)

UIAlertView或ActionSheet已被弃用。你应该真的使用UIAlertController。然后,您可以添加文本字段,按钮作为UIActions,每个操作都有completionHandler,可以轻松地为每个对象实现一个方法。例如:

 UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Test" message:@"Please Enter your name " preferredStyle:UIAlertControllerStyleAlert];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"Person Name ";
        textField.textColor = [UIColor blueColor];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.secureTextEntry = NO;

    }];
    UIAlertAction* yesButton = [UIAlertAction
                                actionWithTitle:@"Yes"
                                style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction * action)
                                {
                                    //Handle the ENTER button 
                                   // how to Display the name in the label

                                }];
    UIAlertAction* noButton = [UIAlertAction
                               actionWithTitle:@"No"
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction * action)
                               {
                                   //Handle no button. 
                               }];

    // adding the object to the box
    [alert addAction:yesButton];
    [alert addAction:noButton];

    // present the box
    [self presentViewController:alert animated:YES completion:nil];