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

后端 未结 1 574
太阳男子
太阳男子 2021-01-13 03:52

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 <

相关标签:
1条回答
  • 2021-01-13 04:37

    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
    
    0 讨论(0)
提交回复
热议问题