Zhen
Zhen

Reputation: 12431

Objective C: Can I set a subview to be firstResponder?

I have a situation whereby I am adding a view from another viewcontroller to an existing viewcontroller. For example:

//set up loading page
self.myLoadingPage = [[LoadingPageViewController alloc]init ];
self.myLoadingPage.view.frame = self.view.bounds;
self.myLoadingPage.view.hidden = YES;

[self.view addSubview:self.myLoadingPage.view];

Is it possible to set 'self.myLoadingPage' to be the first responder? This is the case whereby the loadingpage view size does not cover the entire size of the existing view and users can still interact with the superview (which is not the desired behaviour). I want to just enable the subview in this case.

Upvotes: 3

Views: 1398

Answers (3)

Simon Lee
Simon Lee

Reputation: 22334

The simplest solution is to override hitTest method in your loading view to return TRUE. This top view is first in the responder chain, the hitTest method gets called which NORMALLY returns TRUE if the point is within the view and will therefore be handled, returning TRUE regardless means you get the touch event and effectively block the message being resent to the next responder.

Upvotes: 3

Skyler Saleh
Skyler Saleh

Reputation: 3991

When I had a similar problem, I made an invisible UIView that covered the entire screen, I added the large invisible UIView on top of the main view and made the loading view a subview of the invisible UIView.

Upvotes: 3

Sam
Sam

Reputation: 2707

Interesting question. I found a similar post with a quote from the Apple Developer Forums on this issue:

To truly make this view the only thing on screen that can receive touches you'd need to either add another view over top of everything else to catch the rest of the touches, or subclass a view somewhere in your hierarchy (or your UIWindow itself) and override hitTest:withEvent: to always return your text view when it's visible, or to return nil for touches not in your text view.

This would seem to indicate there isn't a terribly straightforward solution (unless there was an API change regarding this made after October, 2010.)

Alternatively, I suppose you could go through all the other subviews in your superview and individually set their userInteractionEnabled properties to NO (but that would probably prove more cumbersome than the quoted solutions).

I would love to see other ways to allow this.

Upvotes: 2

Related Questions