ios - Enable UIAlertAction of UIAlertController only after input -


i using uialertcontroller present dialog uitextfield , 1 uialertaction button labeled "ok". how disable button until number of characters (say 5 characters) input uitextfield?

add following property in header file

@property(nonatomic, strong)uialertaction *okaction;    

then copy following code in viewdidload method of viewcontroller

self.okaction = [uialertaction actionwithtitle:@"ok"                                          style:uialertactionstyledefault                                        handler:nil]; self.okaction.enabled = no;  uialertcontroller *controller = [uialertcontroller alertcontrollerwithtitle:nil                                                                     message:@"enter text"                                                              preferredstyle:uialertcontrollerstylealert];  [controller addtextfieldwithconfigurationhandler:^(uitextfield *textfield) {      textfield.delegate = self; }];  [controller addaction:self.okaction]; [self presentviewcontroller:controller animated:yes completion:nil]; 

also implement following uitextfield delegate method in class

- (bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string{      nsstring *finalstring = [textfield.text stringbyreplacingcharactersinrange:range withstring:string];    [self.okaction setenabled:(finalstring.length >= 5)];    return yes; } 

this should work


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -