ResignFirstResponder not working. - uitableview

I want to implement a function that once I touch the table view, the search bar on the top of the view will resignFirstResponder and the keyboard will retrieve from the view. I relate the follow code to the tableView in the xib file but it seems not working.
- (IBAction)backgroundTap:(id)sender
{
NSLog(#"test : did touch down");
[_searchBar resignFirstResponder];
}
I try another way of implementation by adding the following code in the .m file, but still not working.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(#"test : did touch down");
UITouch *touch = [touches anyObject];
UIView *view = (UIView *)[touch view];
if (view == self.view) {
[_searchBar resignFirstResponder];
}
}
Hope that someone could help.

Just do..
[Self.view endEditing:YES];

Try doing exactly what you are doing, but call resignFirstResponder twice. I found that I must do that as of iOS 7.

Related

iOS Keyboard doesn't resign until after view transition

I have set this UIViewController to be the delegate for the UITextField in the viewDidLoad with this line: self.nameInputTextField.delegate = self;.
I have set the delegate on the class as well by adding <UITextFieldDelegate> to the #interface declaration.
When I select the nextButton, in the method that is called, I have tried [self.nameInputTextField resignFirstResponder] as well as [self.view endEditing:YES] one line before I push the new view controller.
The rest of the class does not manipulate the firstResponder.
I've also implemented the UITextField delegate method
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self.nameInputTextField resignFirstResponder];
return NO;
}
I haven't found any related questions after extensive searching. There are many similar ones about resigning keyboards, but not regarding the timing of the keyboard resignation being postponed until after the view transition is complete. Note- if you reload this url in your browser, you'll see the gif again from the beginning.
Hide keyboard anywhere in ios :
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UIView * txt in self.view.subviews){
if ([txt isKindOfClass:[UITextField class]] && [txt isFirstResponder]) {
[txt resignFirstResponder];
}
}
}
OR
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
Resign your keyboard on viewWillDisappear and the problem should be solved.
Edit
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
self.nameInputTextField.text = #"";
[self.nameInputTextField resignFirstResponder];
}

How to prevent table view from not selecting the cell when keyboard is up

I have an app where there is a search field on top nav bar. When the user taps on the search field, the keyboard comes up and the background is greyed out. However at this point if the user taps on a cell in the table view, then the keyboard goes away and the cell drills down to the next screen. .how do i prevent this? The only way I can think of is to check if the keyboaard is present and if so then just dismiss the keyboard and do nothing. Is there any other way around it?
I also have buttons in the tableview cell which also activates when it is tapped when the keyboard is up. So I guess I need a generic way of knowing if the keyboard is up and just dismiss and not do any action.
Implement UITextViewDelegate and UITextFieldDelegate
Something like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([theTextView isFirstResponder] && [touch view] != theTextView) {
[tableView setUserInteractionEnabled:NO];
[textField resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
- (void)textViewDidEndEditing:(UITextView *)textView {
[tableView setUserInteractionEnabled:YES];
}

How to send message to UIViewController

I have a problem as following, I've developed a ios app, to be concice , It has a UIViewController as parent, also it has a button , and the UIViewController popup a transparent UIView as a mask. When I click on the UIView(exactly within the underlying button boundary ) , the button could not receive any event(such as "touch up inside"), how could the button get the "touch up inside" event from transparent the UIView which is above the UIViewController?
This is not possible directly as the event triggered will not be for the button as button is not visible
(ie. another View is completly covering the button and is blocking the interaction with the user).
But i can give u a work around.
1.Declare a Bool Variable in your UIViewController
2.Implement the touches methods as shown below
- (void)touchesBegin:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch=[touches anyObject];
CGPoint p=[touch locationInView:self.view];
if(CGRectContainsPoint(button.frame, p) && !boolVariable) {
boolVariable = YES;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch=[touches anyObject];
CGPoint p=[touch locationInView:self.view];
// If the below condition is true then it mean there the user tapped on the same location as that of button..(touchesEnded and touchesCanceled was not called) so the event is just like touchUpInside
if(CGRectContainsPoint(button.frame, p) && boolVariable) {
boolVariable = NO;
[Here you can call the method which you wanted to call on touchUpInside of the button];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
boolVariable = NO;
}
- (void)touchesCanceled:(NSSet *)touches withEvent:(UIEvent *)event {
boolVariable = NO;
}
I was not able to test the able code on Xcode but i think it will work..
Note: The frame of the button should be with respect to the UIViewController.
Hope this helps u out :)
UIView instances, do not listen for touches. In order to get callbacks for events such as "touch up inside" are only sent to subclasses of UIControl
The most basic concrete subclass is UIButton.
Without code or more details about your app's setup, it's difficult to give better advice.

Touch Events for UITableview

I have a UIView overlaying a subclassed UITableview. The problem is that ,I cant get the tableview to scroll. I have tried overriding touchesBegan,touchesMoved,touchesEnded. I then tried to override hittest but that seemed to have no affect.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
NSLog(#"SMTable.touches began %#",NSStringFromCGPoint(touchPoint));
[super touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
NSLog(#"SMTable.touches moved %# for :%p",NSStringFromCGPoint(touchPoint),touch.view);
[super touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
NSLog(#"SMTable.touches ended %#",NSStringFromCGPoint(touchPoint));
[super touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
[super touchesCancelled:touches withEvent:event];
}
- (UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event {
//NSLog(#"SMTable.hitTest %#",NSStringFromCGPoint(point));
return [super hitTest:point withEvent:event];
}
If your UIView is above your UITableView, then all touch events will land in that UIView and your UITableView will not scroll. You need to disable interaction for your top most `UIView˜
When you need to create a specialized UITableView you are almost always better off using a UIViewController that contains a UITableView rather than mucking around in the UITableView hierarchy if at all possible. Apple are doing quite a bit of stuff in the tableview hierarchy which makes adding your own custom views to it often go awry. So, the short answer is : avoid inserting your own views into the tableView hierarchy.
In fact, I almost never use a UITableViewController subclass anymore. I always find myself needing to customize the view controller in a way that isn't easily supported from a UITableViewController-- such as creating a view to overlay the tableView as you are doing. Instead, create your controller like this:
#interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic,strong) IBOutlet UITableView *tableView
#end
If you are using Interface Builder, drop your tableView into the view controller's view and set the delegate and datasource to the view's owner. Or you can do the same thing in code via the viewDidLoad method. In either case, at this point you can treat the view controller exactly as if it were a UITableViewController with the added benefit of being able to do things like inserting views into self.view without things going horribly awry.

hiding keyboard ios [duplicate]

This question already has answers here:
Dismiss keyboard on touch anywhere outside UITextField
(8 answers)
Closed 9 years ago.
I have a few text inputs and I can hide the keyboard whenever I touch the background, but only when I have been entering into the first text box name textField1. now this code should be simple but I just can't seem to get it.
-(IBAction)backgroundTouched:(id)sender {
[textField1 resignFirstResponder];
[buildLength resignFirstResponder];
[buildWidth resignFirstResponder];
[ridgeWidth resignFirstResponder];
[rafterWidth resignFirstResponder];
[hipWidth resignFirstResponder];
[eaveOverhang resignFirstResponder];
[spacing resignFirstResponder];
}
If you want to hide the keyboard when you tap a button and you have more than one UITextFields in your view, then you should use:
[self.view endEditing:YES];
Tap anywhere on the view, and the keyboard will disappear.
Try this:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[[self view] endEditing:YES];
}
You can also iterate through an array of views (such as your UIView's subviews) and manually resign the keyboard, this is good if you dont want to resign on ALL the subviews within your parent UIView.
- (void)viewDidLoad
{
self.view.userInteractionEnabled = TRUE;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//Iterate through your subviews, or some other custom array of views
for (UIView *view in self.view.subviews)
[view resignFirstResponder];
}
You can try UITouch method, and in this set your text field object and call resignFirstResponder
when ever you touch on the screen the keyboard will resign, I hope this will work for you.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[currentSelectedTextField resignFirstResponder];
}

Resources