0

Since most of us create views in code instead of Interface Builder these days, I came to think of what the IBOutlet and IBAction equivalents are in Obj-C.

Is it true that the equivalent of IBOutlet would be equivalent to setting the delegate of the property to the view controller, and IBAction would be equivalent to adding a target action, for example:

rightButton = [UIButton buttonWithType:UIButtonTypeCustom];

//additional junk here

[rightButton addTarget:self action:@selector(firesRightButton) forControlEvents:UIControlEventTouchUpInside];

// off to mission accomplished

If someone could confirm (or just plain state the correct examples), that will be great.

The direct reason for this post is that I see IBOutlet declared in someone's code next to a property, when Interface Builder was not involved at all.

@interface SomeViewController : UIViewController
@property (nonatomic, weak) IBOutlet UISwitch *switch;
@end

What does this switch IBOutlet mean here, if Interface Builder was not even used in the project?

Yes we all know that both IBOutlet and IBAction do not resolve to anything after the pre-processor parses it...

NSCoder
  • 255
  • 5
  • 15
  • If you aren't using Interface Builder (and I don't know if it is true to say that *most* people use code rather than IB) the @IBAction and @IBOutlet are meaningless. The "equivalent" of `@IBOutlet` is just the same property without it and the "equivalent" of `@IBAction` is just a method with the right signature that you set as an action handler. @IBOutlet is completely different to the delegate property of an object. – Paulw11 Apr 14 '16 at 21:32

2 Answers2

1

There is not an equivalent. @IBOutlet and @IBAction are nothing more than special pre-processor directives that tell Xcode that you can wire these things up via interface builder.

So the equivalent of an @IBOutlet is simply any property which I guess you happen to be using in conjunction with your programmatically set up layout.

And the equivalent of @IBAction is simply any method which you wired up to handle a user interaction via addTarget...

nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • Actually `IBOutlet` is a `#define` for nothing. `IBAction` is a `#define` for `void`. They are not "special compiler directives". They are plain old C macros that Interface Builder treats specially. That's it. – rmaddy Apr 14 '16 at 21:42
  • Sorry. Meant pre-processor, not compiler. – nhgrif Apr 14 '16 at 21:43
1

As has already been said by nhgrif, @IBOutlet and @IBAction are just directives for Xcode.

The original developer may have those there as an artefact of a time when it was made using the interface builder. These tags can be left there and should cause no problems when accessing and initialising them from code.

It also means that if the project were to change to using the interface builder, the change could be made with very minimal amounts of code changes.

Matt M
  • 179
  • 1
  • 7