how to resign my keyboard when i stop typing - iphone

i am having 2 textfields, first one name is username and other is password and i want when i enter on password and click outside on view my keyboard should return how is this possible.Can anybody help me.
Thanks

You'll need to define a method on your superview's delegate (usually your current view controller):
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
[self.myPasswordTextField resignFirstResponder];
}
This code tells your password text field (named myPasswordTextField in this example) to resign its first responder status. The first responder is the item handling input at present; when an object resigns first responder, it gives up its input-taking powers, which for a text field means hiding the keyboard and finishing editing.

You can use UITapGestureRecognizer and add it to your [self.view addGestureRecognizer:].
So in your tap gesture you can give the action which you want to perform.
Hope this helps you.

You should use the delegate methods of the UITextField
- (void)textFieldDidEndEditing:(UITextField *)textField{
[textField resignFirstResponder];
}
:)

Related

UITextField - resignFirstResponder Query

I have an application which has a couple of UITextField present to allow my user to enter their age, and another numerical value. Ideally, I want the keyboard to bring up the numeric keypad when the TextField is being edited. At present I have it set to Numer and Punctuation merely to make use of the 'Done' button to dismiss the keyboard as the Numeric pad does not have a done button.
In an attempt to use the Numeric keypad, I have tried to set it to dismiss by tapping the background of my main view.
-(IBAction)backgroundTapped:(id)sender;
I created the above action in my header file.
-(IBAction)backgroundTapped:(id)sender {
[ageEntry resignFirstResponder];
}
I have expanded on the above method in my implementation file to tell the ageEntry TextField to resignFirstResponder. I have also changed my main view to a UIControl class and connected the buttonTapped action to the relevant alert through Interface Builder. Yet when I touch the background nothing happens.
Any ideas?
Just detect the touch in your viewController's view using the method
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[urAgeField resignFirstResponder];
[urOtherField resignFirstResponder];
}
and resign the keyboard in this method
A much easier method is to add a inputAccessoryView to your text field. This input accessory view can be a UIToolbar with a single UITabBarButton for your Done button.
Much less of a hack, and will look like the accessory view that is used in for example Safari to dismiss the keyboard.
i had a similar problem. here is the solution..
EDITED
add
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(tap:)];
tap.numberOfTapsRequired = 1;
[self addGestureRecognizer:tap];
}
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
[ageEntry resignFirstResponder];
}

problem on UITouchs , after touching the out side of the textfield i.e in view key board should disappear

First time the below code works fine ,,,
but after getting back to same view it not working
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[userNameField resignFirstResponder];
[passwordField resignFirstResponder];
[self.view resignFirstResponder];
}
this method is not calling after get back to same view , which i have return this code ...
and my problem after touching out side of the textfield i.e on UIView the keyboard show resignFirstReponder
Try below code
- (IBAction)backgroundTap:(id)sender
{
[yourtextfield resignFirstResponder];
}
In XIB attach this method with TouchDown event of UIView.
Try with removing below from touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event.
[self.view resignFirstResponder];
Form Documentation :
Notifies the receiver that it has been
asked to relinquish its status as
first responder in its window.

Iphone Textview does not invoke TouchesBegan

I have a Textfield which hides the keyboard when I touch somewhere else on the screen (through my TouchesBegan function and resign...blah). But when I touch a Textview the TouchesBegan does not get invoked and the keyboard doesn't hide! Is there any way to invoke TouchesBegan in order to hide the keyboard?
I'm assuming your UITextView is not editable as you don't want the keyboard to pop up when you touch it, so then make sure in the View properties for your UITextView that "User Interaction Enabled" is not checked. You can also write this in your ViewController as such:
textView.userInteractionEnabled = NO;
This will allow user events to pass through to the superview, and touchesBegan and those other delegate methods will be invoked.
There is also a easy way to hide the keyboard on touchesBegan: when you are using UITextView to enter the data.
Step 1. Be sure that you have extended Textviewdelegate while class declaration.
#interface YourClassName : UIViewController {
IBOutlet UITextView *txtMyView; }
step 2. set the delegate to self in view did load function.
- (void)viewDidLoad
{
    txtMyView.delegate = self;
}
step 3. write the similer code in touchesbegan function with you textView name.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
            [txtMyView resignFirstResponder];
[super touchesBegan:touches withEvent:event];
}
Thanks and regards.
The best method I've seen for this is to add a transparent subview that overlays the text view and handles the TouchesBegan first. You can then detect touches outside the text field and dismiss the keyboard by having the text field resign as first responder.
For example, create the overlay subview in IB or programmatically, either way. Place and size it so that it covers the text view(s), and give it a clear color. If you add the view via IB, hide it when your main view loads so that it doesn't absorb touches just yet, like so:
- (void)viewDidLoad
{
[super viewDidLoad];
overView.hidden = YES;
}
Then when the text field begins editing, unhide the view:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
overView.hidden = NO;
}
When the text field ends editing, rehide the view:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
overView.hidden = YES;
}
Add a touchesBegan to detect when your unhidden overView is touched:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
// Resign first responder if touch is outside text field
if ([touch view] != myTextField)
[myTextField resignFirstResponder];
// Send touches up the responder chain (pass them through)
[self.nextResponder touchesBegan:touches withEvent:event];
}
You can also do this through custom gestures, but that only works with iOS 4.x and, in my opinion, is more complicated.

how can i hide the keypad from iphone simulator after its use?

how can i hide the keypad from iphone simulator after its use ?
in my app Ist textfield is for name , another one is for password and one button i want to
hide the keypad whenever i clicked on button.
in button action routine write [yourTextField resignFirstResponder]. it will hide your keyboard.
Jim.
There are 2 method to do it...
1> Implement
- (IBAction) doneButtonOnKeyboardPressed: (id)sender
{
}
Method on Textfield 's Did End On Exit Event
OR
In Textfield implement this delegate method
-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[txtName resignFirstResponder];
return YES;
}
Ok, If we want to close keyboard of simulator when mouse click outside of UITextView (with name "myTextView"), we can use:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.myTextView.resignFirstResponder;
// or [myTextView resignFirstResponder];
}

UITextView has no events

I am developing an iPhone application which requires a multiline text field (UITextView) to capture some text.
When the user touches inside the textView it becomes firstResponder and displays the keyboard. What I really need it to do is remove the keyboard when the user is finished. Normally with a text field the return/done button press would signal the end of typing and I would use the delegate to resign first responder. However with a multiline textview I want the user to be able to add a new line so that is not possible.
My next option would be to resign first responder if there is a touch up outside of the text view. The problem here is that there are no events declared on UITextView which I can see in interface builder.
How can I create a multiline text field in an iPhone app which will release first responder at a sensible time when the user is done with it?
You can use a DONE button on the navigation bar:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneAction:)];
}
to dismiss the keyboard, handle the event with something like:
- (void) doneAction: (id) sender{ [textView resignFirstResponder]; }
(remember .h file declarations)
I'm guessing you are lamenting the "missing" Done button on the keyboard which is return on a multi line UItextView, correct me if i'm wrong.
The obvious approach seems to have another button on your interface that resigns first responder.
I don't think some clever code that hides the keyboard upon some event would be very intuitive to use.
The Apple Notes application uses its top right done button to resign first responder, an approach like this seems sensible.
//If user touches outside of the keypad, then the keypad will go away
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if (touch.tapCount >= 1) {
[ZLabel resignFirstResponder];
}
}
You can create an invisible button that covers all screen and than add a method to it. I think sth like this will work.
- (IBAction)backgroundClick:(id)sender{
[textView resignFirstResponder];
}
If you are completely set on having the 'done' key on the keyboard, the method I use is check the last character in the UITextView when the contents changes. If the last character is a new line, then the user has hit the done button. I would second Neil's view that there should be another button, but sometimes the customer gets what the customer wants.
Obviously you will have to set the delegate for the UITextView to point to the class in which this method lives. The - (void)textViewDidChange:(UITextView *)inTextView method is part of the UITextViewDelegate protocol.
- (void)textViewDidChange:(UITextView *)inTextView {
NSString *text = inTextView.text;
if ([text length] > 0 && [text characterAtIndex:[text length] -1] == '\n') {
message = [text substringToIndex:[text length] -1];
[delegate messageEdited];
[self removeFromSuperview];
}
}
(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
[txtView resignFirstResponder];
return YES;
}