IPhone UIView: is it possible to enable user interaction in subviews only?
I created a view manager that displays a button that is used to slide a view onto the screen. The idea is to place, for example, three of these view monitors on the screen, resulting in three navigation buttons displayed at the bottom of the screen.
The viewcontrollers are stacked, so the top now overlaps other views and therefore other buttons. In this situation, only the top control button of the viewview works, where I would like all three of them to be clickable. I tried to disable user interaction with the view and enable it on buttons only, but it looks like the supervisor settings override the subview settings.
Any ideas?
a source to share
You seem to be confusing views and their controllers: viewControllers are not stacked, they are just part of a nib or xib file. On the other hand, views can be part of a hierarchy of views, and in this sense can overlap. The function of the viewController is to control and coordinate different views on the screen. Using a view controller to control one button is ineffective: the controller's purpose would be to facilitate communication between different buttons (for example, disabling buttons 1,2 and 4 when number 3 is touched). @Bpapa's assertion that using more than one viewController is discouraged by Apple is no longer correct. This section on Windows, Views and View Controllers in the iOs Application Programming Guide states:
The view controller controls a single top-level view directly and can control all or some of these views. For simple user interfaces, the view controller typically manages all of the views in its view hierarchy. However, for more complex interfaces with multiple discrete elements, a view controller can manage a subset of views and rely on one or more custom controller objects to control other view groups in the view hierarchy.
One thing that is not clarified in the docs, but which has bitten me several times over the past weeks, is that creating a viewController only targets a UIControl action if that viewController is the Owner of the file. Let's say, for example, that you have a tip with two viewControllers, gameViewController and scoreViewController. A game view can contain multiple buttons that act as game pieces and touch them, send an action message to the gameViewController's sender method - (void) piecesHasBeenTouched: (id). This works great if gameViewController is the file owner.
Now it might seem logical that if you have a button in the scoreView to let it send a message to the scoreViewController, say saveScore or something. For reasons I don't quite understand yet, the responder chain won't let you. You will need to make a "Save" button in the score view in order to send its action message to gameViewController.
I solve this problem by setting IBOutlet bindings between two controllers - notifications is another possibility.
a source to share
Add this to your code to your costumed UIView
: hitTest: withEvent:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *hittedView = [super hitTest:point withEvent:event];
return hittedView == self.button ? hittedView : nil;
}
I refer to this: Enable UserInteraction for subview only . I checked the code. And it works!
a source to share