I have written a subclass of UIViewController which creates a view programmatically, instead of loading it from a NIB file.
It has a simple loadView
method:
- (void)loadView
{
UIScrollView *mainScrollView =
[[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.view = mainScrollView;
[mainScrollView release];
}
...then I do the bulk of my initialization in viewDidLoad
, as documented. It all works, and I can see the scroll view with my custom views in it.
I need a UIViewController to own the view because it's part of a UINavigationBar workflow. Since I have a controller object, I'd rather it do the controller stuff.
The problem, then, is my view controller does not seem to be in the responder chain. touchesBegan:withEvent:
is invoked if I define it in the root view or a subview, but not if it's in the view controller itself.
Apple event handling documentation glibly mentions the view controller should be in the responder chain. UIViewController documentation says nothing about extra steps needed beyond assigning the root view to the self.view
property, as I've done above. UIResponder documentation claims a UIView should figure out if it has a controller and pass the event to it. UIScrollView documentation says nothing at all.
I've also experimented with various settings of userInteractionEnabled:
for all views and subviews, with no luck.
What am I missing?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…