简体   繁体   中英

IPhone UIView:Is it possible to enable userinteraction only on subviews?

I created a viewcontroller that displays a button which is used to slide a view onto the screen. The idea is to place for instance three such viewcontrollers onscreen, resulting in three buttons at the bottom of the screen providing navigation.

The viewcontrollers get stacked, so to the top one is now overlaying the other views and therefore the other buttons. Only the top viewcontroller's button is working in this situation, where I would like all three of them to be clickable. I tried disabling userinteraction on the view and enabling it on the buttons only, but it looks like the superview settings overrule the subview settings.

Any ideas?

You seem to confuse views and their controllers: viewControllers are not stacked, they are simply part of a nib or xib file. Views, on the other hand, may be part of a view hierarchy and in that sense can overlay each other. The function of a viewController is to manage and coordinate the various views on a screen. Using a view controller to manage a single button is not efficient: the purpose of a controller would be to facilitate the communication between various buttons (disabling buttons 1,2 and 4 when number 3 is touched, for instance). @bpapa's statement that using more than one viewController is discouraged by Apple is no longer correct. The present section Windows, Views, and View Controllers in the iOs Application Programming Guide states:

The view controller manages a single top-level view directly and may manage all or some of that view's subviews. For simple user interfaces, a view controller typically manages all of the views in its view hierarchy. However, for more complex interfaces comprised of several distinct pieces, a view controller may manage a subset of views and rely on one or more custom controller objects to manage other groups of views in the view hierarchy.

One thing that is not made clear in the docs but that has bitten me several times in the past weeks is that making a viewController the target of an UIControl's action only has effect if that viewController is the File's Owner of the nib. Say, for instance, that you have a nib with two viewControllers, gameViewController and scoreViewController. The gameView might contain several buttons that act as game pieces and touching them sends an action message to gameViewController's -(void)pieceHasBeenTouched:(id)sender method. This works great if gameViewController is the File's Owner.

It now might seem logical that if you have a button in the scoreView to let it send a message to scoreViewController, say, saveScore or something like that. For reasons I'm not yet quite clear on, the Responder chain won't let you. You'll need to make the Save button in the score view send its action message to gameViewController.

I solve this problem by establishing IBOutlet bindings between the two controllers -- notifications are another possibility.

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 refered to this: UserInteraction enable for subview only . I've tested the code. And it works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM