iOS UIButton in programmatically instantiated subview not firing(以编程方式实例化的子视图中的 iOS UIButton 未触发)
问题描述
我的故事板中有一个 Nib,它没有连接到任何东西,但我正在将它实例化为子视图:
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 文件,而不是故事板场景;在你加载它并将它放入界面之后,使用代码来配置按钮,以便它与 self
对话,你的 current 视图控制器.
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 未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:以编程方式实例化的子视图中的 iOS UIButton 未触发
- URL编码Swift iOS 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- UITextView 内容插图 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01