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
Post a Comment