Tutorial :iPhone SDK 2: UIButton not working in nested views loaded from nib file



Question:

I have a button in a Nib based view which does not behave like a button (the target method is never called). I have tried to dynamically add the button but it doesnt help.

I will try to describe the app structure (as I suspect it might be causing this issue) and I may not be using the right design pattern.

  • View A is tied to Controller AC. (loaded from a nib in the root controller and pushed on to the navigation controller.)

  • View A has multiple subviews embedded. Lets call one of those A'.

  • In the AC::viewDidLoad method:

    I load 3 viewControllers a,b,c into member variables of AC.

Depending on the app settings I add the view of one of these controllers as the subview of A'

Essentially in the AC::viewDidLoad method:

[A' addSubView:(a.view)];  

Now a.view has a button (which is tagged).I search for that tag. and Add a target Method to it dynamically. At this point the a.view is displayed as subview of A' and the button is also visible. Great.

But touch interaction does not happen or the touch action is consumed by someone other than my method.

I have tried a lot of variations but the execution never reaches my method. I even tried adding the target to all touchUpEvents. nothing. I checked the control values in the enabled events and that is correct. That button is simply disabled somehow. I wrote a simple app with one view and did the same, it worked. So I am thinking it is something to do with the way I am putting multiple pieces together.

Any Ideas?

I wish I had something like Spy++ where I could see the message Queue and see who was consuming this messages instead of my app or even look at the class object. I am a newbie to this platform. My earlier struggle with this is on this thread.


Solution:1

Just enable the UIImageView's userInteraction... don't go thru all that craziness!!!

view.userInteractionEnabled = YES;


Solution:2

Resolved it finally!!! and learnt something in return. Did cost me a few days to figure this out.

The reason my UIButton object was not working was because I found that in case of a UIIMageView object:

"initWithImage: This method adjusts the frame of the receiver to match the size of the specified image. It also disables user interactions for the image view by default."

AND my UIButton had been assigned as a subview of a UIImageView control in the nib file !!!

There was no errors / warnings. It just gets disabled quietly.

Solution: Created a container UIView object which now contains the UIImageView AND the button so that the button appears as overlayed on the Image but it is actually a sibling of the image and a subview of the dummy container UIView.


Solution:3

Quick 'sanity check'... Is the button variable not nil when you are assigning the target/selector?

Does the button appear to press down when you press it, and nothing happens ... or does the button never even press down?


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »