Dismiss the keyboard when the view is hidden [duplicate] - ios

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Easy way to dismiss keyboard?
In view1, the keyboard gets shown. I want to dismiss the keyboard when i move to another view (view2).
In view2, in the method that gets called when the view is shown, i try to do so:
[self endEditing:YES];
But this doesn't work, so i get the idea of catching the event (in View1) of a hidden view and dismiss the keyboard before moving to view2. Is this possible?
EDIT:
I think i need to clarify that view2 is not fully hidden when view1 is shown. It's 50 shown vertically.
As long as i work on view1, the Keyboard is shown and view2 is also shown (50%). view2 has a button, when i click on that button, i need to dismiss the keyboard (which is shown from view1).
I tried to mplement a method in view1 like so:
-(void)dismissKeyBoard{
[self endEditing:YES];
}
And call the method above in view2 when i click on the button but it doesn't work.
PS: The button i click in view2 will make view2 shows fully (100%) above view1.

To dismiss the keyboard, you just have to "resignFirstResponder" on the input field that it is currently in. One easy way is to have a catch all method of all the input fields in your view.
So for example, I usually create a method like this:
-(void)dismissKeyboard {
[self.textfield1 resignFirstResponder];
[self.textfield2 resignFirstResponder];
}
then just call it before you transition
[self dismissKeyboard]

Related

I want the iPhone keyboard to dismiss with the current view controller

Use the slide gesture to pop a view controller, and the keyboard disappears after the view controller disappears.
I want it to disappear together with the view controller like iPhone Message's keyboard.
You need to manually hide the keyboard by calling resignFirstResponder on your textfields or whatever it is you are using to get keyboard input.
Or you can use [self.view endEditing:YES];
Whichever one you choose, you can run it in the viewWillDisappear, that way the keyboard will dismiss along with the view. For example:
- (void)viewWillDisappear:(BOOL)animated {
[self.view endEditing:YES]; //Hides keyboard
[super viewWillDisappear:animated];
}

How can I assign a pointer to the keyboard before I have assigned first responder

I am trying to create a user interface enabling users to switch between the keyboard and other menus when using a chat application.
On a click of the textField bar I want to raise either the keyboard or a collection view.
The problem occurs when I click the 'menu' button. I want the textField bar to raise revealing my menu view. Then, on a click on the keyboard button, instantly switch to the keyboard, rather than having it slide up from the bottom. This means I need to have the keyboard already loaded and hidden but in the background of the app.
Currently though the earliest I am managing to assign a variable to the keyboard is in the keyboardDidShow function.
-(void) keyboardDidShow: (NSNotification *) notification {
// Get the window the keyboard is a subview of
_window = [UIApplication sharedApplication].windows.lastObject;
_keyboard = _window.subviews[0];
}
This means that after it has been loaded once I can hide and reveal it, but I don't want it visible when it is loading this first time.
To achieve this using alternate means I have tried adding my extra views as subviews of the UIWindow the keyboard is created in:
[_window addSubview:_menuView];
[_window addSubview:_gamesView];
[_window addSubview:_stickerView];
[self hideSpecificView];
Unfortunately I keep coming across the same problem, until I have loaded the keyboard once it needs to fully load before I can get a pointer to it to hide it.
Here is a picture of my toolBar incase I am not being clear:
On clicking the menu icon or the stickers icon I want the bar to raise with a collection view. If I then click the textfield, with these views visible, I want to hide the visible view to immediately show the keyboard behind.
I have also tried experimenting with keyboardWillShow but as the window hasn't been loaded in front our screen I can't get a pointer to the keyboard to hide it before it loads.
An example of what I am after can be found many chat apps (facebook messenger, LINE, Kakao Talk)
Any help would be greatly appreciated
Although the way I came up with isn't perfect it works almost perfectly so hopefully this might help people in the future. If anyone else has solved it differently please post as it would be interesting to know how you did it.
I started by adding a class variable to a UIWindow in my header file and then setting off a timer to ping just after the keyboard will show method finishes. After this method has finished the keyboard has been created, just, and so I allocate it and hide it.
-(void) keyboardWillShow: (NSNotification *) notification {
// More keyboard code
_window = [UIApplication sharedApplication].windows.lastObject;
[NSTimer scheduledTimerWithTimeInterval:0.01
target:self
selector:#selector(allocateKeyboard)
userInfo:nil
repeats:NO];
}
- (void)allocateKeyboard {
if (!_keyboard) {
_keyboard = _window.subviews[0];
}
_keyboard.hidden = YES;
[self setViewForButtonType];
}
I have already previously added my other views, hidden them and constrained them to the bottom of the main view, this means that when the keyboard rises they do too.
- (void)viewDidLoad {
[self.view addSubview:_menuView];
[self.view addSubview:_gamesView];
[self.view addSubview:_stickerView];
}
...
- (void)hideViews {
_keyboard.hidden = YES;
_menuView.hidden = YES;
_gamesView.hidden = YES;
_stickerView.hidden = YES;
}
When buttons get pressed I simple then unhide the view that I want to see and hide the rest of the views.
When I say that this method doesn't work perfectly it is because if you load view main view and then click a button before the keyboard has loaded for the first time then you get a quick glimpse of the keyboard before the view appears over the top. This though only happens the first time and only if they don't click in the text field first.
Anyway, I found this was the best way of making views look like they are in front of the keyboard. Obviously my code was a lot longer and more complex (too long for here) but this is the gist of the method I used to solve it. Comment if you have any queries and I hope this helps.

Why can't I CTRL drag an action for storyboard?

I am new to iOS programming and using iOS 6. I see that, using XCode I can CTRL+drag action and outlets for button sand text field but not for the storyboard. I want to do some action when user clicks on the storyboard (taps away from the text field).
Here is my code:
- (IBAction)editingEnded:(UITextField *)sender {
NSLog(#"%#", #"in editingEnded");
[sender resignFirstResponder];
}
- (IBAction)buttonSelected:(UIButton *)sender {
if(_firstClick) {
[_textField resignFirstResponder];
}
}
I guess, you asked me to implement something like editingEnded? This is my delegate for editing did end action (how can I confirm this, there is no such annotation/attribute attached to this method?). However, this method alone didn't work. When i added the 2nd method buttonSelected as a delegate for another button on the story board, then editingEnded is also called due to [_textField resignFirstResponder];.
A storyboard is a container which contains your various UI elements, including your text field.
What you really want to do is set a delegate for your text field, which you can do with your view controller.
Then, when you click away from the text field, you can catch that happening via the delegate method "textFieldDidEndEditing:". You implement that function in your view controller, make certain your text field has your view controller set as the delegate, and you should be able to do whatever you want within your view controller's implementation of the "textFieldDidEndEditing:" function.

How do I hide a button so that it turns see-through rather than white?

I am trying to program a view which has a list of exercises in a table view. The user can either swipe them to delete or go into edit mode and click multiple rows to delete faster (much like in mail).
At the bottom of the screen there is a finish button where the user will click when s/he has finished choosing the exercises they want in their workout. Currently I am trying to bring up the toolbar when the user enters edit mode to allow them to delete their multiple choices. When this happens the view is compressed and so the finish button raises up above the toolbar. This isn't what I want as it looks stupid to be able to finish while editing something else.
I have tried hiding the button but this leaves a white square just above the toolbar
I am not sure if I need to make the button transparent or if I am hiding it incorrectly.
How can I have the bottom button disappear completely when edit mode is entered to the extent it doesn't affect my table view and then reappear and almost 'swap' with the tool bar when editing mode is exited?
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
// Activates multiple selection
exercisesSelectedTableView.allowsMultipleSelectionDuringEditing = editing;
// Lets us know we have clicked editing - changes it to done
[super setEditing:editing animated:animated];
if (editing) {
// This bit is done when someone clicks edit
// Sets the view into editing mode
[exercisesSelectedTableView setEditing:editing animated:YES];
// Unhides the tool bar
[self.navigationController setToolbarHidden:NO animated:YES];
// Hides the finish button
[finishedButton setHidden:YES];
}
else {
// This bit is called once someone clicks done
[self.navigationController setToolbarHidden:YES animated:YES];
// Reveals finish button
[finishedButton setHidden:NO];
[super setEditing:NO animated:YES];
[exercisesSelectedTableView setEditing:NO animated:YES];
}
}
Found the answer to this out eventually.
Using the setToolBarHidden works fine but in the XIB file I needed to check that the table view went all the way down to the bottom of the view. As it was the table view only went to the top of the finished button. Therefore when it was hidden it left the white gap I assumed was the shadow of the button.
Morale of the story: Check the XIB files carefully

How to make to keyboard has behavior like in default apps when text field is inside scroll view?

I have created in storyboard simple app (only one controller), I put scrollview and inside scrollview couple UITextFileds. Inside controller I have added function like
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.name resignFirstResponder];
[self.number resignFirstResponder];
// I have tried with and without this line but doesn't work
[self.scrollView resignFirstResponder];
}
(name, number are Outlets of UITextField, scrollView is Outlet of UIScrollView). When I click on any of those text fields keyboard pops up but when I finish typing I cannot hide keyboard.
(In previous version I didn't have scrollview and keyboard hides when I click out the text field). How to make to keyboard has behavior like in default apps, how to hide ?
I'm assuming you want to just be able to tap away from the keyboard and have it dismissed right? Just do this:
UITapGestureRecognizer *myTapz = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(userTapped)];
myTapz.numberOfTapsRequired=1;
myTapz.cancelsTouchesInView=NO;
[self.view addGestureRecognizer:myTapz];//or do self.WhateverYourOtherViewIsCalled..tableview? scrollView?
[myTapz release];
And then in your selector:
-(IBAction)userTapped
{
[whateverYourTextFieldIsCalled resignFirstResponder];
}
In your view controller:
[self.view endEditing:YES];
This will dismiss the keyboard no matter what field is the first responder. I think there are some exceptions, but for what you're doing it should work fine.
Also touchesBegan is a UIView method, not a UIViewController method. If you're putting it inside your UIScrollView, the scroll view's panGestureRecognizer is going to prevent touchesBegan from being called. Also when overriding touchesBegan, or other touches methods, you typically want to call super as well.
ttarules's suggestion for creating a gesture recognizer is the best way for detecting touches. You can use touchesBegan inside the view, just know that other gesture recognizers can prevent it from being called (see Session 121 - Advanced Gesture Recognition from WWDC 2010).
endEditing is the best way to dismiss the keyboard because it works even after you add other fields.

Resources