2017-01-30 3 views
0

Я хочу отобразить UIActionSheet поверх UIAlertView. Но в соответствии с приведенным ниже кодом он будет скрыт от UIAlertView. (См. Ниже снимок экрана) enter image description hereКак показать UIActionSheet поверх UIAlertView в объекте c

Сначала я показываю пользовательский 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 

} 

Так как я могу отобразить UIActionSheet поверх настроенной UIAlertView ????

+0

Можете ли вы объяснить точный поток вашего кода? Я думаю, что сначала вы вызвали UIActionSheet, тогда ответ на UIActionsheet - это UIAlertView, а в действии UIAlertView вы хотите показать новый UIActionSheet. –

+0

@SiddheshMhatre Сначала я открываю UIActionSheet, а затем выбираю опцию. при выборе опции он будет открыт настраиваемый UIAlertView. После этого, когда я выбираю настраиваемое текстовое поле в UIAlertView (поле единицы), снова откройте таблицу UIActionSheet. Этот UIActionSheet скрыт от предыдущего UIAlerView (см. Прикрепленное изображение). –

ответ

0

UIAlertView или ActionSheet устарели. Вместо этого вы должны использовать UIAlertController. Затем вы можете добавлять текстовые поля, кнопки как UIActions, и каждое действие имеет завершениеHandler, что упрощает реализацию метода для каждого объекта. Например:

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]; 
+0

для iPad, и при использовании UIActionSheet вам все равно нужно будет создать код popover здесь .. просто FYI – cspam

Смежные вопросы