2015-09-29 41 views
5

Please help me with below issue.UIAlertController con UITextfield non visualizza Titolo e messaggio

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Check" message:@"What was the value collected?" preferredStyle:UIAlertControllerStyleAlert]; 
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) 
{ 
    textField.keyboardType = UIKeyboardTypeNumberPad; 
    textField.placeholder = @"What was the value collected?"; 
}]; 

[alertController addAction:[UIAlertAction actionWithTitle:@"Submit" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) 
{     
    UITextField *txtValue = alertController.textFields.firstObject;      
    toCollect = [txtValue.text floatValue]; 
}]]; 
[self presentViewController:alertController animated:YES completion:nil]; 

Ho utilizzato il codice di cui sopra ma mi mostra i risultati nello screenshot qui sotto. Non è possibile visualizzare il titolo e il messaggio
enter image description here

Grazie in anticipo!

+0

ciò che è di raccolta delle? –

+0

la sua variabile float che salva il valore del testo inserito in uitextfield. – BKjadav

+0

il tuo codice funziona bene. –

risposta

8

Qui è il codice per creare UIAlertController per mostrare messaggi e titolo.

UIAlertController *alertController = [UIAlertController 
             alertControllerWithTitle:@"Alert Controller" 
             message:@"Alert Message" 
             preferredStyle:UIAlertControllerStyleAlert]; 

UIViewController *viewController = [[UIViewController alloc]init]; 
[viewController.view setBackgroundColor:[UIColor blueColor]]; 

UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(10, 8, 250, 30)]; 
lbl.text = @"This is a label"; 
lbl.textAlignment = NSTextAlignmentCenter; 
lbl.textColor = [UIColor whiteColor]; 
[viewController.view addSubview:lbl]; 

UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(10, 35, 250, 30)]; 
tf.borderStyle = UITextBorderStyleRoundedRect; 
tf.placeholder = @"Enter your name"; 
[viewController.view addSubview:tf]; 

[alertController setValue:viewController forKey:@"contentViewController"]; 

UIAlertAction *cancelAction = [UIAlertAction 
           actionWithTitle:@"Cancel" 
           style:UIAlertActionStyleCancel 
           handler:^(UIAlertAction *action) 
           { 
            NSLog(@"Cancel action"); 
           }]; 

UIAlertAction *okAction = [UIAlertAction 
          actionWithTitle:@"OK" 
          style:UIAlertActionStyleDefault 
          handler:^(UIAlertAction *action) 
          { 
           NSLog(@"OK action"); 

           NSLog(@"Text Value : %@",tf.text); 
          }]; 

[alertController addAction:cancelAction]; 
[alertController addAction:okAction]; 

dispatch_async(dispatch_get_main_queue(), ^{ 
    [self presentViewController:alertController animated:YES completion:nil]; 
}); 

enter image description here

per SWIFT si può fare stessa cosa come codice seguente:

let alert = UIAlertController(title: "Alert Controller", message: "Alert Message", preferredStyle: UIAlertControllerStyle.Alert) 


let cancelAction = UIAlertAction(
    title: "Cancel", 
    style: UIAlertActionStyle.Destructive) { (action) in 

} 

let confirmAction = UIAlertAction(
    title: "OK", style: UIAlertActionStyle.Default) { (action) in 

} 

alert.addAction(confirmAction) 
alert.addAction(cancelAction) 

let VC = UIViewController() 
VC.view.backgroundColor = UIColor.blackColor() 


let lbl = UILabel() 
lbl.frame = CGRectMake(10, 8, 250, 30) 
lbl.text = "this is a label" 
lbl.textAlignment = NSTextAlignment.Center 
lbl.textColor = UIColor.whiteColor() 
VC.view .addSubview(lbl) 


let txt = UITextField() 
txt.frame = CGRectMake(10, 35, 250, 30) 
txt.borderStyle = UITextBorderStyle.RoundedRect 
txt.placeholder = "enter text" 
VC.view .addSubview(txt) 


alert.setValue(VC, forKey: "contentViewController") 

self.presentViewController(alert, animated: true, completion: nil) 

enter image description here

Scarica Demo da Github: https://github.com/nitingohel/NGAlertViewController-Swift2.0

+0

Grazie per aver lavorato per me !!! – BKjadav

+0

siete i benvenuti e felici di aiutarvi. grazie –

0

Per essere sinceri, il codice funziona correttamente. Solo un sospetto, prova ad aggiungere [self presentViewController:alertController animated:YES completion:nil]; questo pezzo di codice sulla coda principale.

dispatch_async(dispatch_get_main_queue(), ^{ 
    [self presentViewController:alertController animated:YES completion:nil]; 
}); 
+0

mi avvisa: "Questa applicazione sta modificando il motore di autolayout da un thread in background, che può portare a corruzione del motore e crash strani, causando un'eccezione in una versione futura." – BKjadav

+0

vedi risposta modificata – iAnurag

+1

Non capisco, vedo molti postare la domanda con uialertcontroller con lo stesso problema. Sembra bug strano su iOS per loro !!! –

0

Ho modificato il codice un po 'e ha funzionato per me. Fondamentalmente, ho usato il controller "show" invece di "present".

Ecco l'insulsa:

-(IBAction)pressMe:(id)sender { 
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Check" message:@"What was the value collected?" preferredStyle:UIAlertControllerStyleAlert]; 
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) 
    { 
     textField.keyboardType = UIKeyboardTypeNumberPad; 
     textField.placeholder = @"What was the value collected?"; 
    }]; 

    [alertController addAction:[UIAlertAction actionWithTitle:@"Submit" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 

     UITextField *txtValue = alertController.textFields.firstObject; 

     NSString *string = txtValue.text; 
     NSLog(@"String: %@", string); 
    }]]; 
    [self showViewController:alertController sender:nil] 
} 
0

provare questo codice per questo.

if ([UIAlertController class]) 
    { 
     // use UIAlertController 
     UIAlertController *alert= [UIAlertController 
             alertControllerWithTitle:@"Enter Folder Name" 
             message:@"Keep it short and sweet" 
             preferredStyle:UIAlertControllerStyleAlert]; 

     UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault 
                handler:^(UIAlertAction * action){ 
                 //Do Some action here 
                 UITextField *textField = alert.textFields[0]; 
                 NSLog(@"text was %@", textField.text); 

                }]; 
     UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault 
                 handler:^(UIAlertAction * action) { 

                  NSLog(@"cancel btn"); 

                  [alert dismissViewControllerAnimated:YES completion:nil]; 

                 }]; 

     [alert addAction:ok]; 
     [alert addAction:cancel]; 

     [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
      textField.placeholder = @"folder name"; 
      textField.keyboardType = UIKeyboardTypeDefault; 
     }]; 

     [self presentViewController:alert animated:YES completion:nil]; 

    } 
2

mi è stato testato il codice che hai postato qui e questo funziona benissimo su tutti i simulatori e sui dispositivi anche su iOS 8.0+ OPPURE se vuoi testarlo su iOS 7.0, devi usare UIAlertview.

Codice è: vista controller.m

@interface ViewController() 
{ 
    float toCollect; 
} 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

-(IBAction)btntapped:(id)sender 
{ 
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Check" message:@"What was the value collected?" preferredStyle:UIAlertControllerStyleAlert]; 
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) 
    { 
     textField.keyboardType = UIKeyboardTypeNumberPad; 
     textField.placeholder = @"What was the value collected?"; 
    }]; 

    [alertController addAction:[UIAlertAction actionWithTitle:@"Submit" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 

     UITextField *txtValue = alertController.textFields.firstObject; 

     toCollect = [txtValue.text floatValue]; 
}]]; 
    [self presentViewController:alertController animated:YES completion:nil]; 
} 

@end 

Snaps di uscita:

enter image description here