2017-01-30 2 views
0

Je veux afficher UIActionSheet au-dessus de l'UIAlertView. Mais selon le code ci-dessous, il sera caché de UIAlertView. (Voir ci-dessous capture d'écran) enter image description herecomment afficher UIActionSheet sur le UIAlertView dans l'objectif c

D'abord, je montre la UIAlertView personnalisée et lorsque les unités de prise d'utilisateur, puis afficher le UIActionSheet. Mais il est caché de UIAlertView. Voici mon jeu de code.

-(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 

} 

Alors, comment puis-je afficher UIACtionSheet au-dessus du UIAlertView personnalisé ????

+0

Pouvez-vous expliquer le flux exact de votre code? Je pense d'abord que vous avez appelé UIActionSheet alors la réponse pour UIActionsheet est UIAlertView et sur l'action UIAlertView vous voulez montrer une nouvelle UIActionSheet. –

+0

@SiddheshMhatre D'abord, j'ouvre UIActionSheet, puis je sélectionne une option. lorsque vous sélectionnez une option, il sera ouvert personnalisé UIAlertView. Après cela, lorsque je sélectionne un champ de texte personnalisé sur UIAlertView (champ unité), ouvrez à nouveau la feuille UIActionSheet. Cette UIActionSheet est cachée de l'UIAlerView précédente (voir l'image ci-jointe). –

Répondre

0

UIAlertView ou ActionSheet ont été abandonnés. Vous devriez vraiment utiliser UIAlertController à la place. Ensuite, vous pouvez ajouter des champs de texte, des boutons en tant que UIActions et chaque action a completionHandler qui facilite l'implémentation d'une méthode pour chaque objet. Par exemple:

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

pour l'iPad et lors de l'utilisation de l'UIActionSheet vous aurez encore besoin de créer le code popover ici .. juste pour info – cspam