How do I make the keyboard go away when a user clicks on the background of the view? - objective-c

I have a UITextField in my iOS app. When a user enters text and clicks Return, the keyboard goes away due to a call to an IBAction with "resignFirstResponder."
However, XCode does not let me drag a line from the UIView itself to File Owner. How do I associate touching the background of a UIView with an IBAction that makes the keyboard go away?

You can use UITapGestureRecognizer. see: Dismiss keyboard by touching background of UITableView
so instead of tableview, just add it to your view instead:
UITapGestureRecognizer *gestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)] autorelease];
gestureRecognizer.cancelsTouchesInView = NO; //so that action such as clear text field button can be pressed
[self.view addGestureRecognizer:gestureRecognizer];
and have a method to hide your keyboard
- (void) hideKeyboard {
[self.view endEditing:YES];
}

You've already noticed that you can't drag from the UIView to the file's owner to assoctiate an action with a touch.
The way to work around this is to change the class of the background view from UIView to UIControl and hook up an action from there to a method in your controller to stop editing.
That's because a UIControl can respond to touch events, and a UIView does not, but a UIControl subclasses UIView, and so it can be used in place of a UIView.
I wrote an example project a while ago that uses this technique. Have a look at the secondViewController's xib file and see how I've change the class of the background view and hooked it up to a an action in the controller to dismiss the keyboard.

Use the touchesBegan with Event and end editing on the view:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}

One easy way to do it is to create a big transparent UIButton behind the view.

Related

Gesture recognizer for UIView no longer working after connecting the view to IBOutlet

I have a custom button which is basically a UIView with a gesture recognizer. Both the UIView and it's gesture recognizer were put together in interface builder.
The action for the gesture recognizer triggers a segue and opens a view controller. This works just fine.
I created an IBOutlet for this UIView button, but it seems that when I hook it up the action of triggering the segue and opening the next view controller stops working, even though the gesture recognizer and action are still showing up in interface builder.
Why does the gesture recognizer stop working when I hook the UIView up to an IBOutlet?
Check that the User Interaction is enabled for that view in the id-inspector or not. Or check there are no extra connection are shown in the connection inspector
Why you add gesture in your UIView? Simple make your UIView to UIControl class from storyboard and you can simple add action like button on it using storyboard
Or by adding gesture you can do it
connect IBOutlet of your view with UIView
then add gesture in it
-(void)viewDidLoad {
//The setup code (in viewDidLoad in your view controller)
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSingleTap:)];
[vw_button addGestureRecognizer:singleFingerTap];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//The event handling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
CGPoint location = [recognizer locationInView:[recognizer.view superview]];
//Do stuff here...
}

Capture my uibuttons event

I have a big problem in my iphone/ipad ios7 app, I have a lot of controls based on UIView.
for example
list in my view controller I'm adding list based on UIView, this list contains some controls subviews based on UIView, and this controls have a lot subviews (particulary uibuttons) too. And now I want get UIControlEventTouchUpInside action im my viewcontroller, how I can do that ? I Do delegate im my uibutton control but im my view controller I dont't have instance this button, so I can't use
myButton.delegate = self;
I Have just instance my SuperView.
Someone could help me?
It will be better if you use custom view class for your UIView which is you used for containing your button and other controls. In that custom class you can simply set the action for your controls.
EDIT
You can use the following method in your UIButton's custom class:
- (void)awakeFromNib {
[super awakeFromNib];
[self addTarget:self
action:#selector(yourClickMethod:)
forControlEvents:UIControlEventTouchUpInside];
}
- (void)yourClickMethod:(UIButton *)infoButton {
// Button action goes here
}

Questions with interactivePopGestureRecognizer when keyboard shown

This is Screenshot
In View B, When keyboard is shown and pop viewController to View A with interactivePopGestureRecognizer, the keyboard is still stay here :(
How can let keyboard move with view B ?
(like iMessage or Facebook Messenger)
ps: I'm try to get keyboard view and add to self.view, it's useful, but i think it's not a good way.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidShown)
name:UIKeyboardDidShowNotification object:nil];
and
- (void)keyboardDidShown
{
UIView * keyboardView = self.textView.inputAccessoryView.superview;
[self.view addSubview: keyboardView];
}
Try this solution:
https://github.com/cotap/TAPKeyboardPop
This is lightweight category that listens interactivePopGestureRecognizer gesture and animating keyboard accordingly.
You don't add keybordView to the view. If you want to show keyboard for your UITextField/UITextView you call becomeFirstResponder on that object. In your example it should be:
[self.textView becomeFirstResponder];
If you want to hide keyboard you call:
[self.textView resignFirstResponder];
And if you want to hide your keyboard before you dismiss your ViewA you should call it in the beginning of your interactivePopGestureRecognizer when you start animation to show ViewB.
Hope it help.
//EXTENDED
I don't know why you are trying to add accessoryView as a subview to your view
- (void)keyboardDidShown
{
UIView * keyboardView = self.textView.inputAccessoryView.superview;
[self.view addSubview: keyboardView];
}
And I don't understand why do you use notification centre.
The functionality that the keyboard stay when you dismissed the view (as iMessage app) is a standard functionality.
Just use
[self.textView becomeFirstResponder];
when you want to show the keyboard and don't call
[self.textView resignFirstResponder];
when you pop your view controller. iOS does the job for you.

Transparent UIView on top detecting touches

I have an iPad project structured with a UISplitViewController:
RootViewController
DetailviewController
Both of them are detecting touches with Gesture Recognizer inside their own Class.
I would like to create a transparent UIView on top of all the Classes to detect ONLY a Diagonal Swipe (from the left bottom corner to the right top corner).
So, when the swipe will be detected I will launch a function otherwise nothing appended and the touch should be passed on the low level view.
I tried these two solutions:
Add a GestureRecognizer on this top transparent view but this will hide all touches to the lower hierarchy views.( with userInteraction enabled: YES ofcourse);
The other solution is to make the init like this
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.01]];
[self setUserInteractionEnabled:NO];
}
return self;
}
and try to detect the swipe with
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
But at this point all the touches are not detected.
Anybody have a nice solution?
I will not create a transparent UIView like you are mentioning. I will add a UISwipeGestureRecognizer to the UISplitViewController's view this is already the view that contains all your subviews. You can have access to the view within the app delegate:
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
// attach the swipe gesture to the view that embeds the rootView and the detailView
UISwipeGestureRecognizer* swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:splitViewController.view action:#selector(swipeUpdated:)];
Can't you just add a gesture recognizer to the UISplitViewController's view?
You should look into Container Controllers. You can make your own SplitViewController and make a third view on top of the controller that detects the swipe. Custom container controllers are pretty straight forward and gives you a lot of flexibility.

how to detect number of touches on the uiview in iphone sdk

in my app, when the user touches on the view ,i am showing an UIImageView there and i drag and
drop the image from another UIImageView to that touched UIImageView.
But the problem is that, only the recent touched UIImageView is activated. i mean ,when i
click 3 times then shows 3 UIImageViews but only the last is activated and accept the another
image.
How can i make all touched UIImageViews are activated .. Any body help on this..
Thanks in advance.
You should read Apples documentation on the responder chain and event handling. The key UIView method here is
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
This method traverses the view hierarchy by sending the
pointInside:withEvent: message to each subview to determine which
subview should receive a touch event. If pointInside:withEvent:
returns YES, then the subview’s hierarchy is traversed; otherwise, its
branch of the view hierarchy is ignored. You rarely need to call this
method yourself, but you might override it to hide touch events from
subviews.
You should try this:
Where there is your UIView (Or a view):
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapped:)];
tapGesture.numberOfTapsRequired = 1;//number of tap
[view addGestureRecognizer:tapGesture];
The selector:
-(void)tapped:(UITapGestureRecognizer *)sender {
NSLog(#"Pressed");
}