Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 528
Blake Avatar asked Feb 19 '11 22:02

Blake


People also ask

What is IBOutlet and IBAction?

@IBAction is similar to @IBOutlet , but goes the other way: @IBOutlet is a way of connecting code to storyboard layouts, and @IBAction is a way of making storyboard layouts trigger code. This method takes one parameter, called sender . It's of type UIButton because we know that's what will be calling the method.

What is IBAction Objective C?

IBAction – a special method triggered by user-interface objects. Interface Builder recognizes them. @interface Controller { IBOutlet id textField; // links to TextField UI object } - (IBAction)doAction:(id)sender; // e.g. called when button pushed.

What IB stands for in IBOutlet?

IB stands for interface builder, as you connect objects via the interface builder .


1 Answers

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
like image 52
Jacob Relkin Avatar answered Sep 23 '22 08:09

Jacob Relkin