Make UIView work like a UIButton - iphone

I built a Custom Cell in IB and in it created a view that represents a button. It has Labels and ImageViews in it.
I want to know if there is a way to make that view act like a button. That is, show a shadow and call a method when is pressed.

Yes. Just implement tableView:didSelectRowAtIndexPath: and you can do everything you could do in a button action #selector.
As for the shadow and the rounded rectangle button feel, if you use a single cell in a section with UITableViewStyleGrouped, it will be pretty close.

The best is to make it a button. You can use UIButtonTypeCustom and set the background to the cell you've created.

You can use UITapGestureRecognizer and pass the event through a delegate or use UIControl which is a subclass of UIView but with UIButton tap events like touchUpInside and so, and then pass the event with a delegate to your view

Related

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

Why doesn't UIButton showsTouchWhenHighlighted work when the button is on a UITableViewCell?

I put a UIButton on a UITableViewCell and set its showsTouchWhenHighlighted property to YES. The cell's selection style is UITableViewCellSelectionStyleNone. I have a selector that gets called when the button is tapped and it's working fine, so the button is getting the touch events fine. However, button doesn't show the highlight effect when touched.
When I put this button on a UIView, the highlight effect works.
How can I make this work when the button is placed on the cell?
Are you adding the button to the cell or cell.contentView? The contentView property behaves more like a regular UIView, that might solve your problem.
You need to set 'delaysContentTouches' to NO on the Table View

uilabel inside a uiview -- adding gesture to the label not owrking

I have a uilabel (and other labels as well) inside a uiview.
I added a gesture that when the label is tapped on I perform some ibaction.
The action is not triggered, however if i place the label outside the uiview (meanning it's not a child of the uiview) than it works fine. I placed it back inside the view than it's stop triggering the action.
any idea why?
something when the label is inside the uiview makes it not react to the gesture and I'm not sure why (despite the fact that both the uiview and the label have 'User interaction enabled).
TIA
Or just not have a UILabel and have only the text of the button be visible, that way you dont have to have a UILabel and a UIButton (from a memory perspective).
I think the problem here is that the UILabel is receiving the touch event instead of the UIView. Try adding the following code (modified to fit your label, of course) to the viewDidLoad method:
myLabel.userInteractionEnabled = FALSE;
This should allow touch events to pass through to the UIView. Why are you doing this? Might it not be easier to work with a UIButton?
apparently there was an issue with the parent control, I redrew the View and it worked fine. Thanks for your comments.

Objective C: How to make a subclass of UIControl react like a button

I have a subclass of UIControl and within the control I am adding a UILabel and UIImageview to constitute my button.
As a subclass of UIControl, I believe I am unable to utilize the method '[doSomethingButton setBackgroundImage:stretchableButtonImagePressed
forState:UIControlStateHighlighted]' to set the the UIControl to a different image when clicker
How can I mimic a button's reaction to a user's touch i.e. (UIControlStateHighlighted)
Yes, you are right, the setBackgroundImage:ForState method is a method for UIButton.
The easiest to achieve your goal would be to update the imageView && label states in the action selector of your UIControl.
A subclass do all the works which original works. You can directly set the UIImage for highlighted state to your subclassed control.

How do I programmatically make clicking on a UIImageView do something on the iPhone?

I have programmatically added a UIImageView. How do I make it send a message to my controller when the users clicks it, sending itself as the only parameter?
I wouldn't use a UIImageView unless there was some other compelling reason. I'd just use a custom UIButton and use the UIImage as its background.
Create a new class that extends UIImageView and use that.
In your class, override the touch events - touchesBegan, touchesEnded and/or touchesMoved as appropriate
From those methods, call back to a method on the controller.
I'd just put a custom button over the UIImage view, and use normally - buttons with Custom style are invisible and so will not obscure the image. It's also a great way to turn regions of text into clickable areas as well.
Or, you could assign the custom image as a graphic background for the button.
just put a button over the imageview, with the Alpha attribute set to 0.10 or less.
you can capture the button's tap event but almost cannot see it.