Tuesday, 15 January 2013

ios - How to limit character input in UIAlertView UITextField -



ios - How to limit character input in UIAlertView UITextField -

i trying limit number of characters user allowed input uitextfield within uialertview.

i've tried mutual answers on web. such shown below :

#define maxlength 10 - (bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string { if ([textfield.text length] > maxlength) { textfield.text = [textfield.text substringtoindex:maxlength-1]; homecoming no; } homecoming yes; }

anyone able help?

.h file

@property (strong,nonatomic) uitextfield *alerttext; @interface locationsearchviewcontroller : uiviewcontroller<uitextfielddelegate>

.m file

- (ibaction)butpostalcode:(id)sender { uialertview *alrt=[[uialertview alloc]initwithtitle:@"" message:@"" delegate:self cancelbuttontitle:@"cancel" otherbuttontitles:@"ok", nil]; alrt.alertviewstyle = uialertviewstyleplaintextinput; [[alrt textfieldatindex:0] setplaceholder:@"enter postal code"]; alerttext = [alrt textfieldatindex:0]; alerttext.keyboardtype = uikeyboardtypenumberpad; alerttext.delegate=self; alrt.tag=100; [alrt show]; } -(bool) textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string { if(yourtextfieldname.text.length >= 10 && range.length == 0) { homecoming no; } homecoming yes; }

ios objective-c uitextfield uialertview

No comments:

Post a Comment