自定义类中的 UIButton 目标操作 [英] UIButton target action inside custom class
问题描述
我一直在努力解决一个错误,我找到了一个解决方法,但我想了解到底发生了什么.它与 UIButton 目标操作在子类内根据不同的子视图层次结构失火有关.
I've been struggling with a bug, and I found a work-around, but I'd like to understand what is exactly going on. it has something to do with UIButton target actions misfiring depending on different subview hierarchies, inside a subclass.
简要总结:我有一个 NSObject 的子类,它带有一个 UIView 属性对象、一个附加到它的 UIButton 和一个添加到按钮的目标,调用子类中的一个函数.在主 ViewController 中,我初始化了子类并将其视图添加到视图堆栈中,单击按钮,它会将我抛出到 main.mm 并显示错误 - EXC_BAD_ACCESS,给我的反馈很少.所以层次结构看起来像这样:
Brief summary: I have a subclass of NSObject with a UIView property object, a UIButton attached to it, and a target added to the button calling a function inside the subclass. Inside the main ViewController, I init the subclass and add its view to the view stack, click the button, and it throws me to main.mm with the error - EXC_BAD_ACCESS, gives me little feedback. so the hierarchy looks like this:
-CustomClass
--UIView <-this is added as a subview to the View Controller
---UIButton (onRelease calling a function)
所以我通过将自定义类更改为UIView的子类而不是NSObject来修复它,然后将其@property UIView添加为自定义类的子视图(并且按钮仍然附加到子视图),然后在主视图控制器中,我将自定义类本身添加为子视图,而不是类的子视图属性对象.然后按钮成功调用该函数.所以新的安排是这样的:
so I fixed it by changing the custom class to be a subclass of UIView instead of NSObject, then add its @property UIView to be a subview of the custom class (and the button is still attached to the subview), and then in the main View Controller, I add the custom class itself as a subview, not the class's subview property object. then the button successfully calls the function. so the new arrangement looks like this:
-CustomClass (now UIView) <-this is added as a subview to the View Controller
--UIView <-this is added as a subview to CustomClass
---UIButton (onRelease calling a function)
然后,我意识到我可以将 CustomClass 保留为两个实例的 UIView 的子类,如果其他一切都没有改变,原始设置的问题仍然存在.
then, i realized i can just keep the CustomClass a subclass of UIView for both instances, the problem persists with the original setup if everything else is unchanged.
好的,更多细节,这里是代码:
okay, more detail, here's code:
自定义类:.h
@interface Temp : UIView
@property UIView *subview;
@property UIButton *but;
@end
.m
-(id) init{
self = [super initWithFrame:[[UIScreen mainScreen] bounds]];
if(self){
_subview = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//[self addSubview:_subview]; // FOR THE FIX
_but = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_but setTitle:@"OKAY" forState:UIControlStateNormal];
[_but setBackgroundColor:[UIColor blackColor]];
[_but setFrame:CGRectMake(50, 50, 200, 200)];
[_subview addSubview:_but];
[_but addTarget:self action:@selector(pageTurn) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(void) pageTurn{
NSLog(@"WORKS");
}
内部视图控制器:
Temp *temp = [[Temp alloc] init];
[self.view addSubview:temp.subview];
//[self.view addSubview:temp]; // FOR THE FIX, instead of above line
推荐答案
谁在控制 temp
?
如果 temp
没有被任何人引用,那么它就会被释放.那时目标是僵尸,当然你会崩溃.temp.subview
由 self.view
持有.
If temp
isn't referenced by anyone then it's released. At that point the target is zombie and of course you will crash. temp.subview
is being held by self.view
.
在第二个设置中,添加 temp
作为 self.view
的子视图,保持对它的引用.
In the second setup, adding temp
as a subview of self.view
keeps a reference to it.
您可以通过在视图控制器中添加 Temp *
属性来解决此问题.
You can fix this by adding a Temp *
property in the view controller.
self.temp = [[Temp alloc] init];
[self.view addSubview:self.temp.subview];
这篇关于自定义类中的 UIButton 目标操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!