Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

namanhams

macrumors regular
Original poster
Jun 3, 2009
153
0
Let say i have a UIControl. What i want to do is, if the user tap on the control, then some action is executed. If the user tap anywhere else, then the UIControl disappears.

Actually it happens when you tap on a text field, then a menu pop up ("Select", "Select all", "Paste"). If you tap anywhere else, then the menu disappears.

Question is how can i detect that user touch somewhere else outside a specific control ?
 
You place an invisible custom button behind everything and assign an action to it that does what you want. You stretch the button so it fills the entire screen.
 
You place an invisible custom button behind everything and assign an action to it that does what you want. You stretch the button so it fills the entire screen.

That is pointless

@ The OP - Think about it, your UIControl is on a UIView & UIViews inherit from UIResponder. All you need to do is add the touch methods in the views class implementation. All touches outside of the UIControls on the view will be dealt with by the view class then.
 
That is pointless

@ The OP - Think about it, your UIControl is on a UIView & UIViews inherit from UIResponder. All you need to do is add the touch methods in the views class implementation.

Basically what you're describing is a button. So what you're suggesting is write your own UIView subclass that does something when it's touched. Why is writing your own subclass better than just using a button for this?

Also, UIViewController is a subclass of UIResponder. You already have a UIViewController subclass. I'm pretty sure that if your container view doesn't handle a touch it's passed up to the view controller. So add the touch methods there if you're going to add them somewhere.
 
Basically what you're describing is a button. So what you're suggesting is write your own UIView subclass that does something when it's touched. Why is writing your own subclass better than just using a button for this?
I don't think they're suggesting subclassing a UIView. They're suggesting just adding touch handling to the view that is already there. The one that all the other views have been added to.
 
I still think that using a button is the right solution for this. You can have multiple layers of this to have a different effect depending on where the user presses.
 
Basically what you're describing is a button. So what you're suggesting is write your own UIView subclass that does something when it's touched. Why is writing your own subclass better than just using a button for this?

Also, UIViewController is a subclass of UIResponder. You already have a UIViewController subclass. I'm pretty sure that if your container view doesn't handle a touch it's passed up to the view controller. So add the touch methods there if you're going to add them somewhere.

I didn't suggest in any way to subclass a UIView to make it act like a UIButton, that would be utterly pointless. Why don't you make sure you understand my replies before you attack them in future? What I meant is what Dejo interpreted it as, if you didn't already have UIView subclass (Which I assume most people would) then fair enough you could add the methods to the view controller's implementation for the same result.

@ TodVader - Where multiple regions are concerned you could also use CGRects and check if the touch is within them. There is no right or wrong way of doing this. However your method is more memory hungry as you are allocating more objects
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.