touch events on UITextField rightView - iphone

I have several text fields which compromise a registration form . When the user hit submit and made invalid inputs the wrong fields are marked with an icon as rightView.
The rightView i use is a custom UIImageView subclass which has it's:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
overwritten to display a alert to tell the user what exactly is wrong with this field.
The problem is that the touchesBegan method is never called. Why is the view not receiving touch events when I touch it?

you need to enable user interaction of images to TRUE
[imageView setUserInteractionEnabled:YES];

You should check whether your view is on the upper level of views (that it's not overlayed by another transparrent view)
And also check for view's user interaction set to ENABLED
Also check that you tap directly on the view, this method is called only when you tap within your view's bounds
I also recommend you to read a bit about UITapGestureRecognizer class may be this would be more convenient way to use...

Related

resignFirstReponder by touch somewhere outside the UITextView with nested View hierarchy

my first question on stackoverflow...i'm very nervous ;)
Ok, i have troubles to make my UITextView resignFirstResponder on the moment i touch some where on the rest of my view hierarchy.
It's like this:
UIView -> UITableView -> UITableCell -> UIView (my containerview) -> UIView (my bubbleview) -> UITextView
As the UITextView isFirstResponder, we can fill in some text, and keyboard is shown. It would be nice if we do not have to dismiss the keyboard by pressing a button, but just by tapping outside the UITextView somewhere on the surface.
But how and where should i respond to a tap gesture? And on that controller somewhere in the chain. I don't have everywhere a reference to the UITextView to send it a message (resignFirstReponder) to...
I hope i can make myself clear. How can i get rid of of the keyboard by just touch somewhere on the surface....
Hopefully somebody can give some hints...
Regards,
Jeroen
You could use NSNotificationCenter to fire a notification when you touch outside of the text fields bounds to which to your text fields respond to with resignFirstResponder.
Edit with more detail:
You could implement the following method on your UIViewController/View a
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[myTextFeild resignFirstResponder];
...or send a notification etc.
}
Its a bit of a cheap way to pull it off but does work.
You might try subclassing UITextField and overriding pointInside. Examine the touch received and if it is outside of your UITextField subclass instance, then resignFirstResponder.
Edit:
The above actually won't work and I apologize for the bad advice. What I would suggest instead, is adding a transparent subview over the window, which overrides hitTest and calls a predefined callback but returns nil. In the callback, you could perform some arbitrary task (like resigning first responder from your text view) and then remove the view from the hierarchy.
There is a class named CWPrimaryViewWindow in my open source project at https://github.com/Jayway/CWUIKit that is a helper class for implementing this behavior.
The primary idea is to subclass UIWindow in order to hijack all touch events. Then inhibit all touch events not targeted for the primary view, instead firing a cancel notification. While passing through touch events to the primary view as normal.

fire event when touch up inside uiview and outside button

I have a view overlayed to the iPhone's camera view. In this view I have some uibuttons that are displayed in positions depending of values from accelerometer/compass.
I need to fire an event when the user touch up inside the view but he doesn't touch up inside the uibuttons, I mean, the opposite action of touch up inside the uibuttons.
Anyone knows a kind to do this?
Thanks!
If you implement the UIResponder touchesBegan:withEvent: method..
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
...on your view, you'll be able to tell when the user has pressed outside of the buttons.
See the UIResponder Class Reference for more information.
you may e.g. assign one event handler (IBAction) to UiVeiw and its UIButtons and then compare sender parameter:
-(IBAction) viewClicked :(id) sender {
if (id == myView) ...
}
I tried your options but it doesn't work. The view that I tried to fire the event was in a TabBar item in a camera view, so I think the responders chain doesn't work with it.
Finally, I solved the problem doing the overlay view the first responder with:[viewOverlay.view becomeFirstResponder];

User interaction disabled for view but enabled for subviews (UIButtons)

I have a view which contains several UIButtons which overlays a UIScrollView. I want to prevent user interaction on the overlay view but not on the UIButtons that are contained within that view.
The reason why I am grouping these views into a single view is such that I can apply an alpha change to all the buttons in the view by just changing a single property. I have just noticed the IBOutletCollection in IOS 4.0 but I need to also target IOS 3.0.
Is there a more simple way to achieve this than overriding the following UIView method?
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;
Unfortunately if you disable user interaction with a view then user interactions with all subviews are also disabled. While not ideal, you'll just need to make IBOutlets for each of the buttons and adjust them accordingly. To avoid having to write to much additional code in the future, in your viewDidLoad you can create an NSArray and toss each of the buttons into it. Then every time you want to change one of the attributes on all of those buttons, you can just loop over the array and change them. That way if you add another button to the group you only need to update the array and the rest of the changes will automatically propagate.
Please have a look at this answer: https://stackoverflow.com/a/13414182/2082569
According to this you need to override this method in your custom UIView:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *subview = [super hitTest:point withEvent:event];
return subview == self.button ? subview : nil;
}
disable only the scroll view using
scrollView.scrollEnabled = FALSE;
when you want the user to allow usage of button.
Enable again when you want the user to scroll the view using
scrollView.scrollEnabled = TRUE;
Note : you don't need to disable the user interaction of scroll view.

Tapping a UIScrollView to hide the keyboard?

(I posted this before anonymously, but then couldn't use the same computer again, so I'm posting it from my account now. Sorry to the guy who answered before.)
I'm working on an iPhone app which involves typing stuff into a UITextView, which adds content to a UITableView. The problem is, I need to be able to close the keyboard when the user's done with it, and the only area that is really visible other than the keyboard and UITextView at this point is the UITableView. I'm having trouble implementing a touch event on the UITableView (as in, touching the UITableView anywhere, not just didSelectRowAtIndexPath:). Here's the code I'm using in the view controller, but for some reason, it's not being executed at all:
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[textView resignFirstResponder];
}
Any suggestions?
"Here's the code I'm using in the view controller"
That's your problem - you have to create a subclass of UITableView and put the function in there before you will get those touch events.
Put an invisible button over the UITableView and trap the taps.
Yeah, I just said put a breakpoint in to make sure resignFirstResponder is actually being called and that it is being called on the correct text field.

Select tablecell when touching embedded view

I have a custom tablecell with an embedded MapView showing a small area. When the user selects the cell, I want to push a new view with a larger mapview and some more information, like distance from where you are, option of what map-type etc.
If I leave a small margin around my mapview, the user can click in that margin to select the cell, but how can I make the cell selected if they click inside the mapview?
regards,
-Vegar
I think you need to override the hitTest method inherited from UIView
- (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 invoke this method, but
you might override it to hide touch
events from subviews.
Try set MapView's userIteractionEnabled property to NO