How Do I Access IBOutlets From Other Classes in Objective-C?

被刻印的时光 ゝ 提交于 2019-12-01 04:10:58

问题


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?


回答1:


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

Then, in ClassB, you can do this:

@implementation ClassB 

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

...

@end


来源:https://stackoverflow.com/questions/5053988/how-do-i-access-iboutlets-from-other-classes-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!