以编程方式实例化的子视图中的iOS UIButton无法触发 [英] iOS UIButton in programmatically instantiated subview not firing

查看:48
本文介绍了以编程方式实例化的子视图中的iOS UIButton无法触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的故事板上有一个笔尖,该笔尖没有连接任何东西,因此我将其实例化为子视图:

I have a Nib in my storyboard which is not connected to anything but I'm instantiating it as a subview thusly:

-(void)LoadCameraOverlayView
{
    CameraOverlayViewController *cameraVC = [self.storyboard instantiateViewControllerWithIdentifier:@"CameraOverlayNib"];
    cameraVC.view.userInteractionEnabled = YES;
    [self.view addSubview:cameraVC.view];
}

UIViewController的摄像头具有良好的反馈功能,并且有一个按钮(当我向视图控制器进行搜索时也可以正常工作).

The UIViewController has a camera with feedback which is working fine and a button which (when I segue to the view controller also works fine.)

该按钮与"Touch Down"事件和"Touch Up Inside"事件链接.

The button is linked to a Touch Down event and Touch Up Inside event.

当我单击按钮时,我可以看到它正在从默认状态更改为焦点或突出显示状态,但是似乎没有任何代码在执行.如果将NSLog放在ViewDidLoad中,则可以在控制台中看到它,但对于链接方法却看不到.但是,如果我选择该视图,它确实可以工作.

When I click the button I can see it is changing visually from its default state to its focused or highlighted state however none of the code seems to be executing. If I put an NSLog in the ViewDidLoad I can see that in the console but not for the linked method. However if I segue to the view it does work.

这是怎么回事?

我查看了其他解决方案,但我认为可能不是因为该视图存在问题,或者CGRect未正确校准或其他任何原因,因为单击按钮时我可以看到视觉变化.

I looked at other solutions but I don't think it could be that there is a view in the way or that the CGRect isn't calibrated correctly or anything since I can see the visual change when clicking the button.

推荐答案

此代码违反了使用视图控制器的规则之一:

This code violates Rule One of how to use view controllers:

CameraOverlayViewController *cameraVC = [self.storyboard instantiateViewControllerWithIdentifier:@"CameraOverlayNib"];
[self.view addSubview:cameraVC.view];

您不能简单地实例化视图控制器,然后将其视图手动添加到这样的视图层次结构中.对此有非常严格的规则.为了将视图控制器的视图手动添加到界面中,您必须执行一个精心设计的舞蹈",而您并没有在跳舞.

You cannot simply instantiate a view controller and then add its view manually to the view hierarchy like that. There are very strict rules about this; there is an elaborate "dance" that you must do in order to add a view controller's view to your interface manually, and you are not doing the dance.

结果是您的cameraVC出现了,在视图控制器层次结构中没有位置,并且消失了.因此,您的按钮没有对象可与之对话,因此当您轻按该按钮时,什么也不会发生.

The result is that your cameraVC comes into existence, is given no place in the view controller hierarchy, and vanishes in a puff of smoke. There is thus no object for your button to talk to, and so when the button is tapped, nothing happens.

我的建议是:对该视图使用 .xib 文件,而不是分镜脚本场景.然后将其加载并放入界面后,使用代码配置按钮,以便它与您的 current 视图控制器self对话.

My suggestion would be: use a .xib file, not a storyboard scene, for this view; and after you have loaded it and put it into the interface, use code to configure the button so that it talks to self, your current view controller.

这篇关于以编程方式实例化的子视图中的iOS UIButton无法触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆