如何在Objective-C中访问其他类别的IBOutlet? [英] How Do I Access IBOutlets From Other Classes in Objective-C?

查看:97
本文介绍了如何在Objective-C中访问其他类别的IBOutlet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何访问在另一个类中创建的IBOutlets?例如,如果我在Class A中有一个IBOutlet,如何在Class B中访问?如果无法从其他类访问IBOutlets,有什么解决方法?

How do I access IBOutlets that have been created in another class? For example, if I have an IBOutlet in Class A how can I access in Class B? If I can not access IBOutlets from other classes what is a work-around?

推荐答案

您需要将IBOutlet设置为@property并通过@synthesize为该属性定义一个吸气剂,或者您可以定义自己的吸气剂,这是前者的一个示例:

You'll need to make your IBOutlet a @property and define a getter for that property via @synthesize or you can define your own getter, here's an example of the former:

@interface ClassA : NSObject {
   UIView *someView;
}
@property (nonatomic, retain) IBOutlet UIView *someView;
@end

@implementation ClassA

@synthesize someView;

...

@end

然后,在ClassB中,您可以执行以下操作:

Then, in ClassB, you can do this:

@implementation ClassB 

- (void) doSomethingWithSomeView {
   ClassA *a = [ClassA new];
   UIView *someView = [a someView];
   //do something with someView...
}

...

@end

这篇关于如何在Objective-C中访问其他类别的IBOutlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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