I have put a UIView in my navigation item and I am using the JSBadgeView on it. I want for a different screen to appear upon clicking it, but I'm not sure how. iOS won't let me make a seque and I checked off "button" under traits but I'm not sure what to do after that. In Android, I would just do Object.setOnClickListener but as far as I know there is no method like that iOS. Any thoughts?
Create a tap gesture recogniser:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapHandler:)];
Add the tap gesture to the UIView:
[aView addGestureRecognizer:tap];
Then implement the tap handler:
- (IBAction)tapHandler:(UITapGestureRecognizer *)recognizer { // Handler code }
You should just be able to manually perform the required segue in the handler.
A simpler solution would be to implement the touchesBegan method as follows
This will detect when the view was clicked
- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
Related
I have a 3 views in one screen with different components such as tables and buttons, when I try to dismiss the keyboard with the code below it doesn't dismiss when I click on the view that contains the textView( and that’s what I need! ) . However when I click on one of the other views it dismisses.
The following image shows the views that I have and the last view inside the scrollview is the one that contains the textView.
How can I get the keyboard to dismiss despite the view that is taped.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
if(touch.phase == UITouchPhaseBegan) {
[aTextField resignFirstResponder];
}
}
also this code fails to dismiss:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self view] endEditing:TRUE];
}
You need to add tap gesture in the views which you want to click:
For you are using two UIViews with name Your_view1 and Your_view2, and textfields are txt1 and txt2. The code is below:
Your_view1:
UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTapGesture:)];
singleTapGesture.numberOfTapsRequired = 1;
[Your_view1 addGestureRecognizer:singleTapGesture];
Your_view2:
UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTapGesture:)];
singleTapGesture.numberOfTapsRequired = 1;
[Your_view2 addGestureRecognizer:singleTapGesture];
And the delegate is:
-(void)handleSingleTapGesture:(UITapGestureRecognizer *)singleTapGesture{
[txt1 resignFirstResponder];
[txt2 resignFirstResponder];
}
If your textView is editable , tapping on that, will make it first responder. That’s why keyboard is not dismissing. If you don’t need edit textView, set it’s editable attribute to false
You should take a look at Apple document for UIScrollView.
Because a scroll view has no scroll bars, it must know whether a touch signals an intent to scroll versus an intent to track a subview in the content. To make this determination, it temporarily intercepts a touch-down event by starting a timer and, before the timer fires, seeing if the touching finger makes any movement.
That's why touchesBegan doesn't work when you click inside your scroll view.
A easy way to fix it, add a UITapGestureRecognizer for your viewController.view.
#property (nonatomic, strong) UITapGestureRecognizer *tapGestureRecognizer;
- (void)viewDidLoad {
[super viewDidLoad];
self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self.aTextField action:#selector(resignFirstResponder)];
[self.view addGestureRecognizer:self.tapGestureRecognizer];
}
managing keyboard get most of our development time luckly we have an alternative solution with this library
No need to write multiple line of code in every viewcontroller simply add one line of code in your didFinishLaunchingWithOptions
IQKeyboardManager.sharedManager().enable = true
This will save time
Hope this will help you
I am trying to remove the keyboard when it is in editing mode of a textview.I have added tap gesture on the main view.But on the click of when editing mode is for textfield then keyboard is removed but when editing mode is for textview then keyboard is not removed.Please tell me how can i tackle this issue?
added tap gesture to the main view.
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSingleTap:)];
[self.main_view setUserInteractionEnabled:true];
[self.main_view addGestureRecognizer:singleFingerTap];
calling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
if([self.txt_username isFirstResponder])
{
[self.txt_username resignFirstResponder];
}
if([self.txt_password isFirstResponder])
{
[self.txt_password resignFirstResponder];
}
}
I have already set the delegate for text view & also i have added the textview protocol.
delete code for set setUserInteractionEnabled =true , u not need that
and add this in your func
-(void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
[self.view endEditing:YES];
}
or u can use library TPKeyboardAvoiding , set for scrollview..its automatic close keyboard when u tap view..so u not need that UITapGestureRecognizer
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[[self view] endEditing:YES];
}
When there are new or changed touches for a given phase, the app object calls one of these methods. Each method takes two parameters: a set of touches and an event.
For more info Click here
i think you just put only textview resingFirstresponder.
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
[self.txt_username resignFirstResponder];
}
use it.
I'm writing an app in which I want to dismiss the keyboard by clicking in the background. I know I need to type in the following code somewhere after dragging one of the "Sent Events" dots to somewhere on the view controller. Assume for this example that the text field is called textField.
-(IBAction)backgroundTap:(id)sender {
[textField resignFirstResponder];
}
Which dot do I need to drag from in "Sent Events" and to where? I know that this is the code that will work, but I can never remember which dot to use and where to drop it on the screen.
Thank you!
Usually when I need to resign first responder in the event of a background tap, I overwrite the - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event UIViewController method, and in there, I resign first responders. I'm not sure if this is possible using Interface Builder in one quick action.
Use button in background to click out..and write simple code for click EVENT TOUCH UP INSIDE
-(IBAction)backgroundbuttonpress:(id)sender
{
[textField resignFirstResponder];
}
connect action to button...and make sure that You have declared UITextFieldDelegate in .H file
Actually, I remember it now.
I need to use Touch Down with a type of id and call it backgroundTap. Thanks, myself. :P
I would recommend adding a UIGestureRecognizer to you background view. Like this
- (void)viewDidLoad {
[super viewDidLoad];
// Do whatever you want...
UITapGestureRecognizer *screenTapped = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(dismissKeyboard)];
screenTapped.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:screenTapped];
}
- (void)dismissKeyboard {
[self.view endEditing:YES];
}
Now the keyboard will be dismissed when the user taps the background view.
Hope it helps!
There are some answers out there such as this, but in the case where there is a UIScrollView or UICollectionView present, it doesn't work.
The touchesBegan method on the viewController will never get called.
On screen, I have a UITextField at the top.
Below that, filling up the rest of the screen is a UICollectionView.
I need to dismiss the keyboard if I touch anywhere besides the UITextField (including the collection view obviously)
So what is the best way to do this?
For such a common UI paradigm it seems like there should be a well-known solution, but I've yet to come across it.
To dismiss Keyboard on tap of the View: Add a Tap gesture to your ViewController.collectionView as follows:
//declare a property to store your current responder
#property (nonatomic, assign) id currentResponder;
//in viewDidLoad:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(resignOnTap:)];
[singleTap setNumberOfTapsRequired:1];
[singleTap setNumberOfTouchesRequired:1];
[self.collectionView addGestureRecognizer:singleTap];
//Implement the below delegate method:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.currentResponder = textField;
}
//Implement resignOnTap:
- (void)resignOnTap:(id)sender {
[self.currentResponder resignFirstResponder];
}
The simple way to do it is:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
Here's a better solution which doesn't require adding gesture recognisers individually to everything. It's in Swift, but could easily be converted to ObjC.
Add the following to your viewDidLoad():
let tap = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
and then add the following method declaration:
func dismissKeyboard()
{
view.endEditing(true)
}
...where view is your text field.
I've created a mini pop-up menu for the iPhone in a UIView, and I'd like the user to be able to dismiss the view if they do anything other than select one of the options. So, if a user taps/swipes/pinches any other element on the screen, the pop-up view should disappear.
However, I don't want to detect a gesture that will stop something else from happening... For example, there is a UITableView underneath and if I swipe up or down on it, I want it to move as expected as well as dismissing the mini pop-up view.
Should I use multiple gesture recognizers, or should I use touchesBegan, or is there a better way of doing it?
Put this in your UIViewController
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if (touch.view!=yourView && yourView) {
[yourView removeFromSuperview];
yourView=nil;
}
}
EDIT: changes made to detect touch and remove only if view exists
EDIT2: Well you could add the following to your UIButtons/UITableView methods
if (yourView) {
[yourView removeFromSuperview];
yourView=nil;
}
or add touchesBegan:withEvent: as a touchDown event to your buttons.
Both annoying to do but can't see another way to do it as the touchesBegan method doesn't get called with interactive elements.
EDIT3: Right scrap that, think i've nailed it
in your interface add the UIGestureRecognizerDelegate
#interface ViewController : UIViewController <UIGestureRecognizerDelegate> {
then in your viewDidLoad add this
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapMethod)];
tapped.delegate=self;
tapped.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tapped];
then in your viewController add these 2 methods
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if (touch.view!=yourView && yourView) {
return YES;
}
return NO;
}
-(void)tapMethod {
[yourView removeFromSuperview];
yourView=nil;
}