TouchesBegan 在子视图控制器上没有被调用
我有一个 ViewController 可以响应一些 touchEvents(touchesBegan、touchesMoved 等)。
我发现,当我使用presentModalViewController显示此控制器时:它工作得很好,但我试图将其视图添加为另一个ParentViewController的子视图,如下所示:
- (void)viewDidLoad
{
[super viewDidLoad];
//Add SubController
controller = [[SubViewController alloc] initWithNibName:@"SubViewController" bundle:nil];
controller.view.frame = CGRectMake(0, 30, 300, 130);
[view addSubview:controller.view];
[controller release];
}
当我这样做时,它会添加父视图,但它没有对触摸事件的响应时间更长。有办法解决这个问题吗?
另外,有没有更好的方法来解决这个问题?我知道我可能可以为子视图使用 View 子类,但它应该使用 Nib,并且我不确定如何在不使用 ViewController 的情况下处理它。
I have a ViewController that responds to some touchEvents (touchesBegan, touchesMoved, etc...).
I've found that when I show this controller using presentModalViewController: it works just fine, but I'm trying to add it's View as a subview of another ParentViewController like so:
- (void)viewDidLoad
{
[super viewDidLoad];
//Add SubController
controller = [[SubViewController alloc] initWithNibName:@"SubViewController" bundle:nil];
controller.view.frame = CGRectMake(0, 30, 300, 130);
[view addSubview:controller.view];
[controller release];
}
When I do this, it gets added the parent view but it no longer responds to touch events. Is there a way to fix this?
Also, is there a better way to go about this? I know I probably could have used a View subclass for the child view, but it's supposed to use a Nib and I wasn't sure how to handle that without using a ViewController.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你是对的,你应该使用 UIView 子类。
从笔尖加载它的最简单方法是将子视图包含在笔尖中。
只需将 UIView 放入连接到原始视图控制器的视图中即可。
然后选择内部视图后,转到身份检查器。它看起来像一张小身份证。
第一个字段称为自定义类。
在此处键入您的 UIView 子类 的名称。
如果您需要对此的引用,只需在原始视图控制器中创建一个 IBOutlet 并将其连接起来。这样你就可以设置
hidden = YES
直到你需要它为止。在你的 UIView 子类中,你可能想要重写
- (void)awakeFromNib
当笔尖第一次打开包装时,这将被调用。
用于设置任何手势识别器等。
You're correct you should use a UIView subclass.
The easiest way to load it from a nib is to include the subview in your nib.
Just drop a UIView into the view connected to the original view controller.
Then with the view inside selected go to the identity inspector. It's the one that looks like a little ID card.
The very first field is called Custom Class.
Type the name of your UIView subclass here.
If you need a reference to this just create an IBOutlet in your original view controller and hook it up. That way you can set
hidden = YES
until you need it.In your UIView subclass you might want to override
- (void)awakeFromNib
This will get called when the nib first unpacks.
for setting up any gesture recognizers, etc.
要将笔尖直接加载到视图中:
To load a nib directly into a view :
您可以尝试在子视图中调用
becomeFirstResponder
并查看它是否接收到touchesBegan
...可能是这样,但也可能使超级视图接收不到touchesBegan
如果你需要的话...You could try to call
becomeFirstResponder
in your subview and see whether it receivestouchesBegan
... It is probably so, but it will also possibly make the superview not receivetouchesBegan
if you require it...