J'ai une vue d'alerte avec 2 boutons "OK" et "Annuler" et un champ de texte. Je souhaite désactiver le bouton "OK" jusqu'à ce que l'utilisateur entre un texte dans le champ de texte. Comment faire ? Merci d'avance
Réponses
Trop de publicités?MISE À JOUR 2 : pour Swift 5.1
<#your alert controller#>.addTextField {(tf) in
//... set your tf characteristics i.e .keyboardType here
NotificationCenter.default.addObserver(forName: UITextField.textDidChangeNotification,
object: tf,
queue: OperationQueue.main) { _ in
//enable or disable the selected action depending on whether the textField text is empty
<#your alert controller#>.actions[0].isEnabled = !tf.text!.isEmpty
}
}
Je poste ce message pour mettre à jour la réponse depuis ios 5 :
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
UITextField *textField = [alertView textFieldAtIndex:0];
if ([textField.text length] == 0)
{
return NO;
}
return YES;
}
MISE À JOUR :iOS 8 Depuis Apple a déprécié l'UIAlertView en faveur de l'UIAlertController. Il n'y a plus d'appel délégué à alertViewShouldEnableFirstOtherButton:
Au lieu de cela, vous devez définir la propriété "enabled" des boutons via la méthode UITextFieldTextDidChangeNotification
Ajouter un textView à l'alerte avec
- (void)addTextFieldWithConfigurationHandler :(void (^)(UITextField *textField))configurationHandler
[<#your alert#> addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.delegate = self;
textField.tag = 0; //set a tag to 0 though better to use a #define
}];
Mettez ensuite en œuvre la méthode de délégation
- (void)textFieldDidBeginEditing :(UITextField *)textField
- (void)textFieldDidBeginEditing:(UITextField *)textField{
//in here we want to listen for the "UITextFieldTextDidChangeNotification"
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textFieldHasText:)
name:UITextFieldTextDidChangeNotification
object:textField];
}
Lorsque le texte du champ textField change, un appel à "textFieldHasText :" est lancé et une NSNotification* est transmise.
-(void)textFieldHasText:(NSNotification*)notification{
//inside the notification is the object property which is the textField
//we cast the object to a UITextField*
if([[(UITextField*)notification.object text] length] == 0){
//The UIAlertController has actions which are its buttons.
//You can get all the actions "buttons" from the `actions` array
//we have just one so its at index 0
[<#your alert#>.actions[0] setEnabled:NO];
}
else{
[<#your alert#>.actions[0] setEnabled:YES];
}
}
N'oubliez pas de retirer votre observateur lorsque vous avez terminé.
Je voulais compléter la réponse de Ryan Forsyth en ajoutant ceci. Si vous ajoutez un UIAlertView stylé par défaut, vous pouvez obtenir une exception hors plage si vous essayez d'accéder à un champ de texte alors qu'il n'y en a pas, alors vérifiez d'abord le style de votre vue.
-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView*)alertView
{
if(alertView.alertViewStyle == UIAlertViewStyleLoginAndPasswordInput ||
alertView.alertViewStyle == UIAlertViewStylePlainTextInput ||
alertView.alertViewStyle == UIAlertViewStyleSecureTextInput)
{
NSString* text = [[alertView textFieldAtIndex:0] text];
return ([text length] > 0);
}
else if (alertView.alertViewStyle == UIAlertViewStyleDefault)
return true;
else
return false;
}
Sans connaître le contexte de votre demande, il se peut que les points suivants ne s'appliquent pas. Lignes directrices pour l'interface humaine d'iOS ? Il semble que vous feriez mieux de trouver une alternative à UIAlertView si c'est quelque chose qui sera souvent affiché à l'utilisateur.
Ce n'est pas vraiment lié à votre question, mais ne modifiez pas UIAlertView par défaut si vous ne voulez pas que votre application soit rejetée. Si je ne me trompe pas, vous ajoutez des champs de texte à la vue d'alerte, n'est-ce pas ? Comme une vue de connexion. Vous devriez créer votre propre vue.
Donc, en ce qui concerne votre question, créez votre vue, définissez les boutons comme désactivés, et déléguez l'UITextFields. Quand
- (void)textFieldDidBeginEditing:(UITextField *)textField;
est appelé, activer ces boutons.