views:

1527

answers:

4

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.

A: 

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?

Dutchie432
Yes, the button var is ok. No compile warning/error too.The button does not press down. I actually added different pngs to all different states to check that. it doesnt seem to take them at all.
Master Chief
This resembles very closely a problem i had when adding a UIButton to a custom table section header. The problem turned out to that my target/selector was faulty. If I recall correctly, I hadn't declared my selector properly in my .h file. I would go over the target/selector very carefully and make sure everything is correct..http://www.iphonedevsdk.com/forum/iphone-sdk-development-iphone-os-3-0/18343-uibutton-unresponsive-uitableview-custom-header.html
Dutchie432
Method declare as follows - -(void)doAidViewStick:(id)Sender;Actually If I declare it incorrectly in my litle test app, it crashes..I also tried hooking it up with an IBAction in the nib. It just stays disabled for some reason. -(IBAction) doSwitchTohelp;Tearing my hair out! :-(
Master Chief
can you provide some 'dumbed-down' code?
Dutchie432
A: 

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.

Master Chief
A: 

Master Chief --- Can you post your solution code? I am having the same prob

There is no code since I was using a XIB.I simply recreated the view. The button was a subview of a UIImageView earlier, which disables UserInteraction by default. In the New View, I created a UIView and added both the UIIMageView and UIButton as subviews to the UIView. So that they were siblings. So the button was enabled.Hope that helps.
Master Chief
A: 

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

view.userInteractionEnabled = YES;

Shawn Arney