I am trying to dynamically add a UIbutton as a subview to UIlable. But I am not able to click the button. It seems that the label doesn't allow the buttonTapped event to occur.
Can somebody explain what exactly is happening here? and can anybody give me an alternative for this? thanks!
funny, I got answer on my own after 5 mins of posting this question. It seems that I had to enable my userInteraction on the labels.
i.e. -
lbl.userInteractionEnabled=YES;
Your question (and subsequent answer) don't speak to the wisdom (or lack thereof) of doing what you're attempting. UILabel objects aren't generally meant to be interactive, and placing a UIButton as a subview to the label, just because you can do it, doesn't sound at all like it's following the Apple HIG for iPhone.
Related
I'm an iPhone dev newbie, and I'm having a problem slightly similar to a few posts I read around here, but none of them seemed to help.
I have created a UIScrollView, with a UIView as its content (a subview). During runtime, the UIView is populated with labels and buttons. Everything looks fine, scrolling works perfectly, but the UIButtons never fire when pressed, no matter what I do. I've tried many combinations of properties suggested here, to the best of my understanding, but still nothing worked. I'm afraid I might have misunderstood something about the mechanism.
I should mention that everything is done in code (no IB).
Any suggestions?
My bug (written in the comments of my question) was not setting the frame of the contentview that was the parent of all buttons. The result was strange - I could see the button subviews (which was why it took me a while to find the bug) but could not click on them. oops!
This also may happen, when your custom UIView userInteractionEnabled is NO (default is NO).
try [btn becomeFirstResponder];
I have a subview that acts as a container view in a table header. In that, I have a UIButton. The button is not receiving any touch events. (Yes everything is wired up properly in IB...)
So my question is, is it a common problem to have buttons not receiving any events in the header? Do I need to forward any events?
I can't really post any code, since it would appear to be more of an IB problem. Anyone experience this before?
UPDATE: If I put the button in the footer, with no container view, it will work. So perhaps it's because it is in a view inside the header??
Wow, I feel like a moron. I didn't have user interaction enabled. Sorry for wasting all your time guys...
I've included UIButtons and even UITableViews in my table header before, so I don't think it should be an issue. Something is just not setup correctly. I didn't have to do anything special to get touch events to fire.
I have run into an interesting bug where it appears that when you select a UITableCell it changes all of it's subviews to their highlighed state. This includes my accessoryView which is a UIButton which obviously I only want highlighted when the user presses like a normal button.
I have a friend who went as far subclassing UIButton to override this in his app and manually managed the state for this type of behavior but that seems drastic to get what should be the expected behavior. So, I am wondering what other solutions people have come up with for this problem.
Has anyone else had this issue? If so, what did you do to work around it? Any code samples as well as ideas are very welcome!
I ended going with a custom button on this like Jesse Recommended abd set both the highlighed and normal state to the same image. It seemed to work well enough for what I was trying to do. Thanks again!
I have been browsing SO and various other sites to find out whether this is possible, it definitely seems to be!
The problem I have is every "tutorial" seems to state that the UIButton needs to be inside a UIView of some kind and I can't quite figure out how to get the XIB together to allow the animation.
Any and all help would be greatly appreciated!
just place a button in another view, the animate it's position.
see examples in here: http://github.com/neror/ftutils
I have a UIButton that selects itself on UIControlEventTouchUpInside. It deselects itself on UIControlEventTouchUpOutside. I also want it to deselect itself when there is a touch down outside of the button. Is there a good way to do this without subclassing UIWindow and overriding -hitTest:withEvent:?
EDIT: I just found this question, which confirms my fear that there isn't a really clean way to do this.
This is not how buttons are supposed to work. They cannot be active if there is no touch active inside them. What are you really trying to implement? Maybe a UIBUtton is not the best choice here.
You can do this with your UIViewController by implementing the touchesBegan/Moved/Ended methods. If they detect a touch outside of the button, set its state back to normal. Take a look at Event Handling.
I solved this, and released the source. I didn't have to subclass UIWindow, but I did need to override -hitTest:withEvent:.