Hide keyboard when touch uitableview - iphone

I have a custom cell with a uitextfield inside. I want to hide the keyboard when the user touch the screen, I put a custom uibutton over my tableView, and in the touch up inside event, I call
-(IBAction) hideKeyBoard
{
[customcell.textfield resignFirstResponder];
}
is it the right way to hide the keyboard with a uitableview because it don't works

No, a UIButton over your tableview is going to obstruct touches to the table, and views with alpha less than something like 0.1.
One method would be to subclass UITableView and override touchesBegan to detect a touch. From there, you have many options for how to deal with resigning first responder, notification, delegate method, reference to the text field.

Related

iOS UIScrollView cancel UIButton touch on scroll

I have some UIButtons within a UIScrollView, but I do not want to delay the button touches. However, as soon as the scroll view detects a drag/scroll, I want to cancel the UIButton touch and proceed with the scrolling of the UIScrollView.
I have included the following...
_scrollView.delaysContentTouches = NO;
...but (obviously) the button touch does not cancel when dragged. Does anyone have any suggestions as to how I can implement this functionality?
You can override this method of UIScrollView.
-(BOOL)touchesShouldCancelInContentView:(UIView *)view
{
return YES;
}
And in your UIButton, you should add different method for different control events.
show highlight effect for UIControlEventTouchDown.
trigger button for UIControlEventTouchUpInside | UIControlEventTouchUpOutside;
clear highlight effect for UIControlEventTouchCancel.
You could use scrollViewWillBeginDragging to fire off a notification and handle the button canceling by listening for it in your buttons' code. I think this is what you are trying to do, but I'm not sure if I have understood your question correctly.

How do I tell the difference between the user tapping my table view cell and the user tapping a subview contained within the cell?

One of my view controllers contains a UITableView with custom UITableViewCell's. My custom UITableView cell contains a UIImageView subview that represents a tappable icon. When the user taps anywhere on the custom cell, except the icon subview, I want my didSelectRowAtIndexPath method to be called like normal. But when my icon subview gets tapped, I want a different method to get called, but I can't figure out how to do this. Do I have capture the touch position in the touchesDidBegin method and manually check if the user tapped the icon? That just feels so hacky. Other, cleaner ideas?
Thanks so much for your wisdom!
Simply add a tapGestureRecognizer to the UIImageView in your cell

Touch on UIButton in a UITableViewCell cannot scroll the table

I need to have a UIButton inside a UITableViewCell, everything works fine except when I touch inside the button and move my finger, the table does not scroll.
I tried to subclass UIButton and in touchesMoved: method send the same message to self.nextResponder, or call touchesCancelled and hope it will pass the touch event to next responder, they both do not work.
Is there any good way to solve this problem? Currently I am adding a UIView on top of the UITableViewCell, and detecting touch events manually, passing result to the button or the rest of the cell respectively, but this is a little bit dirty.
What you can try is setting the delaysContentTouches property of the UITableView to YES.
Additionally you can set the canCancelContentTouches to YES.
If the value of this property is NO, the scroll view does not scroll
regardless of finger movement once the content view starts tracking.
Source: UIScrollView Class Reference
Try:
_yourTableView.delaysContentTouches = YES;
_yourTableView.canCancelContentTouches = YES;
The best way is to make a custom cell and do your work neatly.
Check the Apple docs

Multiple UIText fields with Multiple UIPickerViews in UITableView

I have a grouped style UITableView with a HeaderView that is loaded from another .xib.
The HeaderView has 4 UITextFields in it:
2 of the fields should display the Keyboard and allow user input.
2 of the fields should display a UIPickerView and update the UITextField with selection.
The Main TableView (with the sections and rows) is filled with UITextFields as well.
The UITextField in the first section (indexpath.section = 0) displays a UIPickerView
All other UITextFields in the rest of the sections/rows should display the Keyboard
I can get the Keyboard to display correctly and dismiss when the Done button is touched for all the UITextFields that can display the keyboard.
I can get the UIPickerView to display correctly and dismiss (with Custom Save/Cancel buttons).
The problem I have is when mixing the two...
When I do the following I have a hybrid effect:
Step 1: Touch the first UITextField to begin to enter data with the Keybard.
Step 2: Enter some data in the UITextField.
Step 3: Touch a UITextField that displays a UIPickerView instead of the Keyboard.
The result is the Keyboard AND my custom UIPickerView being displayed at the same time with the Keyboard actually displaying ON TOP of my UIPicker!
Any suggestions on how to prevent this from happening?
Need to show some code, but most likely you're not calling [myTextField resignFirstResponder] just before calling the UIPickerView.
You need to resign your keyboard for your textField. From the fact that you did not do it, I am guessing that you did not resign the pickerView as well. So you need to resign both of them or they will continue to stay on the screen
Resigning the textField.
Implement UITextFieldDelegate
Implement
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return 1;
}
Resigning pickerview:
Implement the picker view as your property and implement [self.yourpickerview resignFirstResponder] in textFieldDidEndEditing and in the IBAction of your custom DONE button for your picker view.

How to retract the Keyboard on touching the UITextView when keyboard is already up?

I am writing an application that has a UITextView which allows editing. When a user first touches UITextView, a keyboard shows up and I want to retract that keyboard when user again touches the UITextView e.g. I have entered some data in a textview and with keyboard still showing on the screen I tap on the UITextView which should cause the keyboard to retract.
Is there any way to achive this?
(I'm aware of providing a done button and doing this but I want to achive this by tapping on UITextView itself)
As an aside, I would urge you not to use toggle state elements on the iPhone. It's to easy to double tap in real world use. That is why the Apple apps all use the either the "return" key on the keyboard or the done button.
In the interface you contemplate, the users will find themselves closing and then accidentally reopening the keyboard about 10% of the time or more. It will make your app feel cumbersome and flaky.
You should call resignFirstResponder for the UITextView. Let's say you have an IBOutlet for the text view:
#property (nonatomic, retain) IBOutlet UITextView *comment;
Then [comment resignFirstResponder]; can be called from a touchesBegan or the like.
See e.g. How to Dismiss the Keyboard when using a UITextView.
What you are looking for is a large transparent uibutton "overlapButton" which always stays on top of the uitextview.
When the textview appears, you set the button hidden so you can tap on the textview freely.
[overlapButton setHidden:YES];
When tapping the textview, the keyboard will come up and the following method inside your textview delegate will get called:
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
[overlapButton setHidden:NO];
}
Here, you need to set visible the "overlapButton" so that while the keyboard is up, you can touch the button which now overlaps the textview. On the button action, you can hide the keyboard:
-(IBAction) overlapButtonTapped{
[myTextView resignFirstResponder];
}
After resigning the first responder, the following method will get called:
-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
[overlapButton setHidden:YES];
}
After setting the hidden property accordingly for the button (like above), you have a "clear" textview again which you can tap again to show the keyboard.. etc .. etc ..
Cheers.
I agree with you TechZen. I've seen this happen! I would not advise this also! It also makes editing very hard if not impossible for edit/copy/paste gestures. On the other hand, if the man still wants this badly.. :P the code provided by me in my other post works a treat! Cheers.