2016-03-22 2 views
1

Il semble qu'il y ait un certain nombre de questions demandant cela, mais rien que j'ai essayé n'a fonctionné pour moi. Simplement, je voudrais un UIAlertView avec UIAlertViewStylePlainTextInput mais avec la boîte de saisie préchargée avec une chaîne donnée quand elle est affichée - pas comme un espace réservé, mais comme une entrée par défaut. Est-ce possible?Le texte par défaut dans UIAlertVIew sur le spectacle

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"message" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 
[[alert textFieldAtIndex:0] setText:[NSString stringWithFormat:@"%@", @"preloaded string"]]; 
alert.alertViewStyle = UIAlertViewStylePlainTextInput; 
[alert show]; 

L'exemple ci-dessus utilise setText qui ne fonctionne pas. J'ai aussi essayé:

[[alert textFieldAtIndex:0] setPlaceholder:@"text"]; 

juste pour l'intérêt, mais cela n'affiche même pas l'espace réservé alors peut-être que je suis absent quelque chose d'autre.

Des pointeurs?

+0

Essayez le réglage de la 'alertViewStyle' avant? – Larme

+4

UIAlertView est obsolète depuis iOS8. Vous devriez passer à UIAlertController. Dans ce cas, vous pouvez faire comme 'UITextField * alertText1 = alertController.textFields.firstObject; [alertText1 setText @ "Default Txt"]; ' –

+0

@Larme Cela fonctionne - merci. Ajouter comme réponse et je vais le choisir. – RGriffiths

Répondre

0

vous devez soit créer votre propre point de vue « pop-up » personnalisé qui ressemble UIAlertview comme dans here ou utiliser une implémentation open source comme SDCAlertView

ce que vous pouvez faire est d'utiliser UIAlertController (comme UIAlertview est également obsolète dans l'IOS 8)

UITextField *alert = alertController.textFields.firstObject; 
[alert [email protected]"YOUR ALERT TEXT HERE"]; 
0

qu'il fonctionne à 100%

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"message" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 
alert.alertViewStyle = UIAlertViewStylePlainTextInput; 
[[alert textFieldAtIndex:0] setText:[NSString stringWithFormat:@"%@", @"preloaded string"]]; 
[alert show]; 
0

Utilisez UIAlertController à la place:

let alertController = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert) 
alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in 
    // Configure UITextField here... 
    textField.placeholder = "Placeholder" 
} 
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (_) -> Void in 
    let textField = alertController.textFields![0] as UITextField 
    let value = textField.text ?? "Untitled" 
    /// Save value here... 
} 
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil) 
alertController.addAction(okAction) 
alertController.addAction(cancelAction) 
presentViewController(alertController, animated: true, completion: nil) 
0

vous pouvez le faire comme

AlertView

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"sample" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Done", nil]; 

    [alert setAlertViewStyle: UIAlertViewStylePlainTextInput]; 
    // Alert style customization 

    [[alert textFieldAtIndex:0] setDelegate:self];  
    [[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad]; 
    [[alert textFieldAtIndex:0] setText:@"Richard"];   
    [[alert textFieldAtIndex:0] setPlaceholder:@"textvalue"]; 

    [alert show]; 

UIAlertController

UIAlertController *alert = [UIAlertController 

           alertControllerWithTitle:@"sample" 

           message:@" " 

           preferredStyle:UIAlertControllerStyleAlert]; 



    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) 

    { 


     [textField setText:@"Richard"]; 
     textField.placeholder = NSLocalizedString(@"textvalue", @"Login"); 


    }]; 
    [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
// do your action on ok click 

    }]]; 

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


    // do your action on cancel click 
    }]]; 

    UIViewController *viewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController]; 

    if (viewController.presentedViewController && !viewController.presentedViewController.isBeingDismissed) { 

     viewController = viewController.presentedViewController; 

    } 



    NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:alert.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:viewController.view.frame.size.height*2.0f]; 

    [alert.view addConstraint:constraint]; 



    [viewController presentViewController:alert animated:YES completion:^{ 



    }];