简体   繁体   English

iOS-从其超类设置UIButton目标和操作

[英]iOS - set UIButton target and action from its super class

I have 2 classes ClassA: UIView ClassB: UIViewController 我有2个类ClassA:UIView ClassB:UIViewController

I have a UIButton in ClassA 我在ClassA中有一个UIButton

The button is declared in the header and as an @property of A 该按钮在标题中声明为A的@property

UIButton *selectBtn;    
}
@property (nonatomic, strong) UIButton *selectBtn;

in the .m file of Class A the button is initialized and shows up on screen 在A类的.m文件中,按钮已初始化并显示在屏幕上

ClassB imports ClassA's header file and from it I'm trying to set the target and the action for my button with the following ClassB导入ClassA的头文件,并从中尝试用以下命令设置按钮的目标和操作

[ClassA.selectBtn addTarget:self action:@selector(getSomething:)     forControlEvents:UIControlEventTouchUpInside];

But i keep getting an Error message saying "Property "selectBtn" not found on object of type "ClassA" 但是我不断收到一条错误消息,提示“类型A”的对象上找不到“属性“ selectBtn”

What am I doing wrong ? 我究竟做错了什么 ? any help or guidance is very appreciated 任何帮助或指导都非常感谢

[ClassA.selectPicBtn addTarget:self action:@selector(getSomething:)     forControlEvents:UIControlEventTouchUpInside];

使用selectPIcBtn而不是selectBtn

I don't know if I'm missing something because none of the other posters brought this up but from what I see selectBtn is a property of a ClassA instance, not a property of the class itself. 我不知道我是否缺少任何东西,因为其他海报都没有提出,但是从我看来selectBtn是ClassA实例的属性,而不是类本身的属性。 Try grabbing an instance of a ClassA object and then accessing the property from that instance: 尝试获取ClassA对象的实例,然后从该实例访问属性:

//WRONG
[ClassA.selectBtn addTarget:self action:@selector(getSomething:) forControlEvents:UIControlEventTouchUpInside];

//Correct
ClassA *aClassAObject = [[ClassA alloc] init];
[aClassAObject.selectBtn addTarget:self action:@selector(getSomething:) forControlEvents:UIControlEventTouchUpInside];

Please keep in mind, the above code is just an example and is not likely the object you are looking for. 请记住,上面的代码只是一个示例,不太可能是您要查找的对象。 If you are doing this inside the ClassB object you have to grab the ClassA UIView that is actively being used by the ClassB UIViewController. 如果要在ClassB对象中执行此操作,则必须获取ClassB UIViewController积极使用的ClassA UIView。 Typically, I would create a property in ClassB to access the ClassA object similar to the way you created a property for a UIButton object but I'm not sure how you have your classes setup without seeing the header files. 通常,我会在ClassB中创建一个属性来访问ClassA对象,类似于为UIButton对象创建属性的方式,但是我不确定如何设置类而不看到头文件。

Problem is here. 问题在这里。

UIButton *selectBtn;    
}
@property (nonatomic, strong) UIButton *selectPicBtn;

selectBtn and selectPicBtn are different. selectBtn和selectPicBtn不同。 property name should be same as variable name. 属性名称应与变量名称相同。 If you have declared a property then you don't need variable declaration. 如果声明了属性,则不需要变量声明。

Only declare property and use it. 只声明属性并使用它。 While initializing, initialize self.selectPicBtn = [UIButton.... in Class A 初始化时,初始化Class A self.selectPicBtn = [UIButton....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM