Iboutlet and Ibaction in Swift

Place references to IBOutlet and IBAction inside viewDidLoad or just before?

ViewDidLoad is only one method of setup. You can put some code here, local variables, ets. @IBOutlet and @IBAction are links of Interface Builder and nameLabel. And this outlet and action suppose to be global, so you could use them in other functions, not only in viewDidLoad.

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var nameLabel: UILabel!

override func viewDidLoad() {
super.viewDidLoad()

nameLabel.text = "my name is Cyril"
}

}

Difference between IBOutlet and IBAction

Number 1 google response: IBOutlet and IBAction

Yes, you can make an IBOutlet a property, and you need to release anything that you retain, so if the property is set with 'retain' attribute, then you'll need to release it.

Models for developing WCF applications

This article is great in terms of how to set up your project structure the right way:

http://code-magazine.com/Article.aspx?quickid=0809101

What does IB mean in IBAction, IBOutlet, etc..?

"Interface Builder".

Before Xcode 4, the interface files (XIBs and NIBs) were edited in a separate program called Interface Builder, hence the prefix.

IBAction is defined to void, and IBOutlet to nothing. They are just clues to Interface Builder when parsing files to make them available for connections.

Just to add the reference, inside AppKit/NSNibDeclarations.h you'll find these:

#ifndef IBOutlet
#define IBOutlet
#endif

#ifndef IBAction
#define IBAction void
#endif

So, actually, code like this:

@interface ...
{
IBOutlet NSTextField *label;
}
- (IBAction)buttonPressed:(id)sender;
@end

Will be transformed into:

@interface ...
{
NSTextField *label;
}
- (void)buttonPressed:(id)sender;
@end

By the preprocessor, even before the compiler sees it. Those keywords were acting just as clues to Interface Builder.

IBOutlet and IBAction

IBAction and IBOutlet are macros defined to denote variables and methods that can be referred to in Interface Builder.

IBAction resolves to void and IBOutlet resolves to nothing, but they signify to Xcode and Interface builder that these variables and methods can be used in Interface builder to link UI elements to your code.

If you're not going to be using Interface Builder at all, then you don't need them in your code, but if you are going to use it, then you need to specify IBAction for methods that will be used in IB and IBOutlet for objects that will be used in IB.



Related Topics



Leave a reply



Submit