i have a screen having navigation controller and text field. when i move next and come back i want the keyboard should be hidden in first screen. I am hiding keyboard like on textfield event.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
But how to do that in View related events so that whenever my view appears keyboard is hidden..
Pls guide/Help.
thanks in adv.
I think this is also a good way to remove keyboard with in iOS App if your UITextView or UITextField not connected through the IBOutlet.
If you want to Hide Keyboard with UIViewController LifeCycle Events like with viewWillAppear or etc. Follow this
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[self view] endEditing:YES];
}
Otherwise if you object connected using IBOutLet this code will work fine as you describe too.
[yourTextField resignFirstResponder];
Add this code to your ViewWillAppear :
for(id obj in self.view.subviews)
{
if([obj isKindOfClass:[UITextField class]])
{
[obj resignFirstResponder];
}
}
This would take in all the textfields in that particular view here it is the whole view and add the code you had written previously for removing the keyboard.
A good habit is to write this code in your screen's -viewWillDisappear. So, when you navigate from one screen to another at that time it will remove the keyboard from that screen.
- (void)viewWillDisappear:(BOOL)animated
{
[self.view endEditing:YES];
[super viewWillDisappear:animated];
}
For multiple textFields, it is better to use -endEditing for that particular view instead of -resignFirstResponder for any single textField. Take a look at my Answer.
//This is for Swift
override func viewWillAppear(animated: Bool)
{
self.view.endEditing(true)
}
The thing that you are doing wrong is , when you are moving back previous controller to the current controller , the keyboard is up due to the selected textfield of previous controller .
And in the current controller the code:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[self view] endEditing:YES];
}
It will not work as no textfield is selected at this controller. So what you need to do is write the same code in the previous controller viewWillDisappear Method it will surely resolve your Problem .
- (void)viewWillDisappear:(BOOL)animated
{
[self.view endEditing:YES];
[super viewWillDisappear:animated];
}
Related
I'm trying to hide the iPad keyboard on a modal view, which has the FormSheet style. I tried to resignFirstResponder, but nothing happens. Is this a bug or doesn't this work at all?
best regards
EDIT
-(void)hideKeyboards
{
[emailField resignFirstResponder];
[passwordField resignFirstResponder];
[confirmPasswordField resignFirstResponder];
}
-(IBAction)emailFieldDone:(id)sender
{
[self hideKeyboards];
}
-(IBAction)passwordFieldDone:(id)sender
{
[self hideKeyboards];
}
-(IBAction)confirmPasswordFieldDone:(id)sender
{
[self hideKeyboards];
}
Overriding disablesAutomaticKeyboardDismissal to return NO as below fixed the same problem of mine. You need to override disablesAutomaticKeyboardDismissal of UINavigationController, not the own view controller, to fix this issue. Maybe use a category is a good idea:
- (BOOL)disablesAutomaticKeyboardDismissal {
return NO;
}
Also, check this iPad keyboard will not dismiss if modal view controller presentation style is UIModalPresentationFormSheet question if you want to get a detailed explanation.
try this if you are using textview
Textviewobjectname.editable = NO;
Set delegates to all text field
textField.delegate=self;
I have two textfields which are of type UITextField.
I have two buttons click and cancel.
The idea is to enter two numbers and it will navigate to next view. This View should display those two numbers.
The problem is if I click on the textfield one then the keypad is responding; when I click return it does not jump to the next field.
-(BOOL)textFieldShouldReturn:(UITextField *)textField{[textField resignFirstResponder];return YES;}
You need to connect UITextFiled Delegate from nib or programeticuly like this image:
or
other way is programmatic like in your viewDidLoad method or viewWillAppear method:
Yourtxtfild.delegate = self;
EDIT
for jumping AnotherView Use this textfiled delegate method:-
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
//you can set your textfiled string in to globle Variable and use it in another viewController
YourViewCntroller *objViewController = [[YourViewCntroller alloc] initWithNibName:#"YourViewCntrollernib" bundle:nil];
[self.navigationController pushViewController:objViewController animated:YES];
}
You have to set the delegate of the textfield as the object of the class where you have written the textFieldShouldReturn: method.
#Ragavan : You are just resigning the first textfield, but you are not making the other textfield to respond. So, use the code below as reference to make it work.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField1 resignFirstResponder];
[textField2 becomeFirstResponder];
return YES;
}
I am making an application with TabBar. But TabBarController is not RootViewController. In the Tab Bar there are 4 tabs. One of those tabs is history which is linked with a table view which shows history. I want to refresh that view every time when i click that so that i can get updated tableview. How can i do that? Thanks in advance.
use - (void) viewWillAppear:(BOOL)animated to update any content in your view.
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// You code here to update the view.
}
This method will be called every time the view is about to be displayed.
below code added plz ^^
If you change a tabBarIndex, At the same time -(void)viewWillAppear called.
-(void)viewWillAppear:(BOOL)animated
{
// force the tableview to load
[tableView reloadData];
}
refer a Apple Sample Code: that is amazing great tutorial for you about UITabBarController
http://developer.apple.com/library/ios/#samplecode/TheElements/Introduction/Intro.html
Apple Sample Code is no added [super viewWillAppear:animated];
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
if ([Globals sharedInstance].isFromFindSimiler)
{
[self.view addSubview:_findSimilerView];
[_historyTableView setFrame:CGRectMake(0, 85, 320, 490)];
}
else
{
[self history_webservice];
}
}
I have achieved this. In this case, whenever user tabs on History screen I call web service for history and update tableview.
instead of using (void)ViewDidLoad use viewWillAppear:(BOOL)animated
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//your code
}
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];
}
I have one view name:myplay.h and myplay.m
my view contain one textfield name txtplay..
It contain one button name btnplay.
In button event i want to check that if uikeyboard is open then close it.
I know below code
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return TRUE;
}
and in button click event
-(IBAction)btnplayclick:(id)sender
{
[self.txtplay resignFirstResponder];
....
....
}
I want a global code to resign.
Try this:
[self.view endEditing:YES];
From the doc:
endEditing:
Causes the view (or one of its embedded text fields) to resign the first responder status.
(BOOL)endEditing:(BOOL)force
This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign.
Available in iOS 2.0 and later.
The best way i think to be generic and reuse your code anywhere is to create a category for the UIView :
#implementation UIView (FindFirstResponder)
- (UIView *)findFirstResonder
{
if (self.isFirstResponder) {
return self;
}
for (UIView *subView in self.subviews) {
UIView *firstResponder = [subView findFirstResonder];
if (firstResponder != nil) {
return firstResponder;
}
}
return nil;
}
#end
And then to call the method like that :
-(IBAction)btnplayclick:(id)sender
{
UIView *firstResponder = [self.view findFirstResonder];
[firstResponder resignFirstResponder];
}
It does the tricks perfectly for me.
or just write
[self.view findAndResignFirstResponder];
Calling resignFirstResponder on a UITextField that ISN'T first responder is a harmless no-op.
So go:
for (UIView *candidate in self.subview) {
if ([candidate isKindOfClass:[UITextView class]]) {
[(UITextView *)candidate resignFirstResponder];
}
}