shouldChangeCharactersInRange method not called after calling becomeFirstResponder - objective-c

Hi i am new to the iOS,
i am using several text fields in my project, so i want use becomeFirstResponder for editing done for any one of text field. it is working fine in xcode 3.2.5 but it makes problem in xcode 4.2.1
In xcode 4.2.1 after calling the becomeFirstResponder i could not edit the text field, it is showing the virtual key board i could tap the keys but tapped keys are not entered in text field.
Please help me out.

Firstly add this delegates in interface:
#interface yourViewController : UIViewController<UITextFieldDelegate>
If using from xib then right click on UITextField and bind delegate with fileowner else
add this code in viewDidLoad's method.
yourTextField.delegate = self;
Add UITextField's delegate and it will be called
- (BOOL)textFieldShouldReturn:(UITextField *)textField //resign first responder for textfield
{
[textField resignFirstResponder];
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
return YES;
}

Related

Programmatic UITextField and programmatically releasing the keyboard

I've looked in many places and it seems everyone uses IB. I like it, but find it more fun writing it all out. This being said, I'm having difficulty dropping my keyboard after editing is done. Here's an example.
-(void)textStuff
{
UITextField *someField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];//not true size
}
Here I've tried the resign firstresponder and even a BOOL to say are you editing, YES, then make that keyboard first responder dangit....oh, you're done editing...good, now resign that first responder.....none has worked as of yet. Any help would be greatly appreciated.
Thanks
1).
[self.view endEditing:YES];
2).you set the delegate method.
SOMEFIELD.text =delegate;
And .h file
#interface yourViewController : UIViewController<UITextFieldDelegate>
and in .m file you add the delegate method of the textfield
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
all you have to do is set someField's delegate your view controller and implement following method,
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}

How do I find out which text field is responding to Textfielddidend

I have multiple uitextfields but they all respond to the delegate the method. I tried using textfield description but that doesn't work. There must be a way to know which text field is active but I can't find it.
thanks.
According to the UITextFieldDelegate protocol reference, all the methods pass the UITextField that's calling the method. For example:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
You can just check the text field against a known one:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField == self.fooBarField)
{
//do magic
}
return YES;
}

UITextView trouble in xcode

So i was reading the description about UITextView's and it says that it automatically hides the keyboard when you press the 'Return' button on the keyboard. But it wasn't working, so I tried creating an
- (IBAction)textViewReturn:(id)sender;
{
[myTextView resignFirstResponder];
}
That did not work either so i tried also doing:
- (BOOL)textViewShouldReturn:(UITextView *)textView
{
[myTextView resignFirstResponder];
return NO;
}
Not sure why the whole deal isn't working in the first place. Wondering if anyone could help?
I don't see anything in the UITextView Class Reference that says it automatically hides the keyboard when you press Return.
Also, there is no textViewShouldReturn: message in the UITextViewDelegate protocol. There is a textFieldShouldReturn: message in the UITextFieldDelegate protocol, but a text view is not a text field.
If you want it to hide the keyboard when the user presses Return, you need to do two things.
First, you need to connect some object - usually your view controller - to the text view's delegate outlet. You can do that in your nib, or you can do it in code, perhaps in your viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
myTextView.delegate = self;
}
Second, you need to implement the textView:shouldChangeTextInRange:replacementText: in your delegate object:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
} else {
return YES;
}
}
Note that if the user pastes in text containing a newline and other characters, this will not catch the newline. It will only notice when the user either taps the Return key, or when he pastes in text containing just a newline.
You can declare the delegate's class as conforming to the UITextViewDelegate protocol, in which case Xcode will helpfully autocomplete the method name. But it will work even if the class doesn't conform to the protocol.
Return button of UITextView is used to mote the cursor to the new line. But if you want to remove the keyboard on return button then please try the following code. It resigns the keyboard when return button is pressed by user. So try following code which definitely solved your problem.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
} else {
return YES;
}
}
Per your comment, if you want to use a UITextField instead of a UITextView, then things remain the same except that in order to hide the keyboard when you hit return, you need to implement the following function in the text field's delegate (make sure that you have set this first):
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}

How can i change text field without a button action

For objective-C and iOS development, Is it possible to change textfield when user pushes a key.
For ex : User pushes the key 'k' but sees 'a' in the textfield. (the same usage in PeterAnswers.com )
Yes, I think this is possible. You should take a look at this UITextField delegate method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
You should be able to manipulate the input string before it is displayed in the actual textfield. A specific implementation of your question could be:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
// if the user entered x...
if ([string isEqualToString:#"x"]){
textField.text = [NSString stringWithFormat:#"%#a", textField.text]; // ...append the previous string with the 'a' token instead!
return NO; // return NO to make sure the x isn't put in the textField
}
}
Don't forget to make sure to link the UITextField delegate to the class where you put this code.
Hope it helps!
Implement the textField:shouldChangeCharactersInRange:replacementString: method of the UITextFieldDelegate protocol.
Totally. Look at the apple documentation on UITextFieldDelegate. Everything you need is right there.

How to hide the keyboard in objective-c

I have a simple view with a textbox and a UIButton. When I click the UIButton I simply want to hide the keyboard that is currently in the view. Is this a simple delegate I can add to the controller or something more complex?
Of the answers that exist on SO already I haven't found one that has a full solution for this context. Any help would be great!
Try something like:
[TextField resignFirstResponder];
Where TextField is the name of your text field.
This is how you hide the UITextField when you hit the return button:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// do whatever you have to do
[textField resignFirstResponder];
return YES;
}
This is how you hide when you hit an UIButton:
- (void)hideTextField:(UITextField *)textField {
// do whatever you have to do
[textField resignFirstResponder];
}