Update UISteppers value from textfield - textfield

here is the case.
I would like to type a value in a text field and pass it to the current uisteppers value.
if for example current value is 2 and i enter 35 in the textfield then when i press the + button on the stepper the value should go to 36 (or by pressing - to 34)
Thanks
--------------------------------------EDIT AFTER 1st REPLY----------------------------------
thanks for fast reply, since i am fresher to all this i declared
#interface GenericViewController : UIViewController <UITextFieldDelegate> {... in my header. and changed my code inside Generic Controller to:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
[textField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
NSLog(#"######PASS DELEGATE ###############");
return YES;
}
- (void) textFieldDidChange:(id)sender {
UITextField *textField = (UITextField *)sender;
textField = changeNfpHand;
NSLog(#"##############################");
NSLog(#"textfield is now #------> : %f" , DateStepper.stepValue);
self.DateStepper.stepValue = [textField.text doubleValue];
// self.DateStepper.stepValue = [self.Nfp.text doubleValue];
NSLog(#"VALUE STEP is #------> : %f" , DateStepper.stepValue);
NSLog(#"self.Nfp.text #------> : %#" , self.Nfp.text);
Now I am new to all this.. i tried to call [self textFieldDidChange:self]; but no luck. it seems that i cant get it work...any ideas of what am i doing wrong ? Thanks in advance

In the delegate of UITextField, add the following code, which will make the UIStepper's step value be changing according to what you enter in UITextField.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
[textField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
return YES;
}
- (void) textFieldDidChange:(id)sender {
UITextField *textField = (UITextField *)sender;
self.stepper.stepValue = [textField.text doubleValue];
}

Related

When UITextField is editing, call setText will hang a while

I am formatting text of UITextField when inputting. Code:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *newString = nil;
// format 'newString' here
[textField setText:newString];
// move cursor
return NO;
}
But setText: will hang for a while in main thread. Less text more time.
So, I tried below code, resignFirstResponder before setText::
[UIView setAnimationsEnabled:NO];
[textField resignFirstResponder];
[textField setText:newString];
[textField becomeFirstResponder];
[UIView setAnimationsEnabled:YES];
It resolved the hang issue.
But why it will hang. And If there is any better solution.
What's newText? Do you mean newString (which you define as nil). Notice UITextField's text isn't nullable.

resignFirstResponder not getting called inside textFieldShouldClear

I am trying to implement search bar in one of my page.
I am not using regular search bar due to its design.
What I have is as below.
UIImageView above UIView (textfield background)
UITextField above UIImageView (textfield)
I am using delegates for UITextField.
In code I have searchTF.clearButtonMode = UITextFieldViewModeWhileEditing; to show the clear button.
Search is working fine but the problem is in delegate of clear button.
I have below code
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if (textField == searchTF) {
NSLog(#"clicked clear button");
[textField resignFirstResponder]; // this is not working
// also below is not working
// [searchTF resignFirstResponder];
}
return YES;
}
When I click clear button, I get NSLog of text "clicked clear button", however the keyboard doesn't get dismissed.
Any idea why keyboard is not getting dismissed when I have
Edit 1
Even I tried as below using [self.view endEditing:YES];, but still its not working.
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if (textField == searchTF) {
[self.view endEditing:YES];
[self hideAllKeyboards];
}
return YES;
}
Additional to my comment I made some testing and here are my results:
I've just implemented a UITextField with all delegate methods like this:
- (BOOL)textFieldShouldClear:(UITextField *)textField {
NSLog(#"Should Clear");
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
NSLog(#"Begin editing");
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSLog(#"End editing");
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
NSLog(#"Should begin editing");
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
NSLog(#"Should end editing");
return YES;
}
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
NSLog(#"Change char");
return YES;
}
As soon as you hit the clear button the log outputs:
2014-07-26 11:08:44.558 Test[36330:60b] Should Clear
2014-07-26 11:08:44.558 Test[36330:60b] Should end editing
2014-07-26 11:08:44.559 Test[36330:60b] End editing
2014-07-26 11:08:44.560 Test[36330:60b] Should begin editing
2014-07-26 11:08:44.561 Test[36330:60b] Begin editing
As you can see the shouldBeginEditingand the didBeginEditing methods get called after clearing so the resignFirstResponder in textFieldshouldClear gets called just before a new becomeFirstResponder is called by shouldBeginEditing or didBeginEditing.
I believe your textfield IBOutlet is properly connected ,Although you can try
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
I am not sure what was the problem, but I solved calling the dismiss keyboard method after some interval using NSTimer.
Below is what I did.
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if (textField == searchTF) {
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(hideAllKeyboards) userInfo:nil repeats:NO];
}
return YES;
}
-(void) hideAllKeyboards {
[searchTF resignFirstResponder];
}
This way, after clicking the clear button, keyboard is getting dismissed.
It would be great if someone post answer as why this is happening. I will mark that answer as accepted.
Edit 1
As per Daniel answer, I did below.
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if (textField == searchTF) {
isFilterOn.text = #"no";
[textField resignFirstResponder];
textField.text = #"";
[self myTextFieldDidChange];
}
return NO;
}
In myTextFieldDidChange, I am showing the filtered list and un-filtered list based on the text I have in UITextField.

When textfield become first responder, gesture recogniser not responding, even after resign

I've probably missed something...
First, I inherited from UITextField and added a Tap gesture recogniser to a UITextField (in the designated initialiser):
UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(pressed:)];
[self addGestureRecognizer:ges];
-(void)pressed:(id)sender
{
didPressed = YES;
[self becomeFirstResponder];
}
Then I set my viewController to be the textField delegate and implemented this:
- (BOOL)textField:(UIOneLetterTextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSLog(#"Key Pressed %#", string);
textField.text = string;
[textField resignFirstResponder];
UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:textField action:#selector(pressed:)];
[textField addGestureRecognizer:ges];
[self gotoNextTextfield:textField.cellLoc];
return NO;
}
From this point, for some reason, pressed: doesn't get called when tapping on the textField.
Any Idea why?
The delegate should implement
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
and return YES.
Is there a specific reason why you are using a UITapGestureRecognizer? The UITextFieldDelegate has a methods that are called whenever a textField starts editing :
textFieldShouldBeginEditing:
textFieldDidBeginEditing:
Unless you have some code that conflicts with these methods, you do not need a UITapGestureRecognizer

Date and time masking with text field

Hello everyone,
I am working on a project where requirement is keypad should not pop up when we clicked on the text field as we are making number pad for the particular text field and we have succeed for doing this by..
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (textField == dateFld) {
UIView* dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
textField.inputView = dummyView;
}
}
Now my problem is, I want to validate that text field to accept only particular format and limited number of input but i am unable to do this because the method for that is not getting called when we disabled the keypad to pop below is my code to validate the textfield.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//Format Date of Birth YYYY-MM-DD
if(textField == dateFld)
{
if ((dateFld.text.length == 4)||(dateFld.text.length == 7))
//Handle backspace being pressed
if (![string isEqualToString:#""])
dateFld.text = [dateFld.text stringByAppendingString:#"-"];
return !([textField.text length]>9 && [string length] > range.length);
}
else
return YES;
}
Please help me to over come from this problem or any other way to do this.
Thanks
I want to validate that text field to accept only particular format and limited number of input :
if(textField == dateFld){
NSCharacterSet* numberCharSet = [NSCharacterSet characterSetWithCharactersInString:#"0123456789+"];
for (int i = 0; i < [string length]; ++i)
{
unichar c = [string characterAtIndex:i];
if (![numberCharSet characterIsMember:c])
{
return NO;
}
}
//Format Date of Birth YYYY-MM-DD
if([textField.text length] == 4) {
textField.text=[NSString stringWithFormat:#"%#-",textField.text];
}else if([textField.text length]==6){
textField.text=[NSString stringWithFormat:#"%#-",textField.text];
}else if([textField.text length]==8){
textField.text=[NSString stringWithFormat:#"%#",textField.text];
}
NSLog(#"value %#",textField.text);
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 13) ? NO : YES;
}
}
It's working fine for me.
I would do something like this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSError *_error = nil;
NSRegularExpression *_regularExpression = [NSRegularExpression regularExpressionWithPattern:#"^\\d{0,4}-{0,1}$|^\\d{4}-{1}\\d{0,2}-{0,1}$|^\\d{4}-{1}\\d{2}-{1}\\d{0,2}$" options:NSRegularExpressionCaseInsensitive error:&_error];
NSMutableString *_newText = [NSMutableString stringWithString:textField.text];
[_newText insertString:string atIndex:range.location];
NSArray *_matches = [_regularExpression matchesInString:_newText options:0 range:NSMakeRange(0, _newText.length)];
return _matches.count > 0;
}
EDIT#1 (on 22/01/13)
of course, your class has to be a delegate class of your UITextField, otherwise the method above will be never called back.
there are some additional steps how you can do it.
in the YourViewController.h file:
#interface YourViewController : UIViewController <UITextFieldDelegate> {
// ...
}
#property (nonatomic, strong) IBOutlet UITextField *myTextField; // is case of ARC and iOS5+
#end
in your YourViewController.m file
- (void)viewDidLoad
{
[super viewDidLoad];
// ...
[self.myTextField setDelegate:self]; // you can do the same thing in the interface builder as well.
}
You need to validate the inputs in your dummyView
UIView* dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
textField.inputView = dummyView;
since you replaced the input view of UITextField, UITextField delegates will not be called.

UITextField in a UIActionSheet only calling some delegate methods

The below code shows that when a user does a long press gesture on a Table View Cell, then a UIActionSheet launches with a UITextField inside of it. When tapping the UITextField, the keyboard launches, and textFieldShouldBeginEditing and textFieldDidBeginEditing get called, but the text field won't accept the key taps.
Hitting the return key won't trigger the delegate methods, but tapping one of the UIActionSheet buttons will trigger textFieldShouldEndEditing and then textFieldDidEndEditing.
I'm setting the textField to become the first responder, so I'm not sure why it's not accepting input from the keyboard. Any suggestions?
- (void)longPress:(UILongPressGestureRecognizer *)gesture
{
// only when gesture was recognized, not when ended
if (gesture.state == UIGestureRecognizerStateBegan)
{
// get affected cell
SinTableViewCell *cell = (SinTableViewCell *)[gesture view];
// get indexPath of cell
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// do something with this action
NSLog(#"Long-pressed cell at row %d", indexPath);
AppDelegate_Shared *appDelegate = (AppDelegate_Shared*)[UIApplication sharedApplication].delegate;
//setup UITextField for the UIActionSheet
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 170, 320, 200)];
textField.borderStyle = UITextBorderStyleBezel;
textField.backgroundColor = UIColorFromRGB(0XFFFFFF);
textField.text = #"";
textField.delegate = self;
[textField setKeyboardType:UIKeyboardTypeAlphabet];
[textField setKeyboardAppearance:UIKeyboardAppearanceAlert];
//setup UIActionSheet
UIActionSheet *asheet = [[UIActionSheet alloc] initWithTitle:#"Add Notes"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles: #"Save", nil];
[asheet showFromTabBar:appDelegate.tabBarController.tabBar];
[asheet setFrame:CGRectMake(0, 100, 320,380)];
[asheet insertSubview:textField atIndex:0];
//[textField becomeFirstResponder];
//memory management
[textField release];
[asheet release];
}
}
#pragma mark -
#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
}
#pragma mark -
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
NSLog(#"textFieldShouldBeginEditing");
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
NSLog(#"textFieldDidBeginEditing");
[textField becomeFirstResponder];
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
NSLog(#"textFieldShouldEndEditing");
return YES;
}
//should save the notes value here, I think
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSLog(#"textFieldDidEndEditing");
[textField resignFirstResponder];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField {
NSLog(#"textFieldShouldClearEditing");
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(#"in textFieldShouldReturn");
return YES;
}
Your question is a little bit old but not marked as answered, so if it is helpful for you or other viewers I post my solution from my own SO question. I started up with the same problem as you had and ended up in the fact that UIActionSheet really eats up some important events which are necessary to get the keyboard working properly.
My linked posted code unfortunately works only in portrait orientation, anyway it does it.
Is UITextFieldDelegate and UIActionSheetDelegate in your header?
If not, there could be problems while setting the textfield.delegate to self