Absolute beginner: How do I read this...

Joined
Feb 14, 2013
Messages
4
Reaction score
0
Points
1
I'm new to ios deveolpment, but have a little prior knowledge in programming .net 1.0/2.0.
Mij OOP skills are at beginner level still. I'm trying to learn to xcode using the book Objective-c for absolute beginners.

I struggle mainly with the syntax of objective-c. I'm learning class basics and ui basics.
An example from the book has me build an small app with just a label and a button. When the button is pressed, the label gets updated.

Now, in the viewcontroller.h file I define the following property:

Code:
@interface ViewController : UIViewController {
    IBOutlet UILabel *nameLabel;
}

- (IBAction)showName:(id)sender;

How do I read that line. The type is UILabel, the name is *nameLabel, but what is IBOutlet and why does that need to come in front of the property?

Also, in the implementation file the label is set in the showName method

Code:
- (IBAction)showName:(id)sender{
    [nameLabel setText:@"My name is ..."];
}

Why do I also need to link the label to the owner in the UI (xib file)?

Thanks for any answers!


EDIT:
I hope these beginners questions are okay here ...
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,762
Reaction score
2,100
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
First, these questions are perfectly fine here..

IBOutlet and IBAction are special markers for Interface Builder. In code, they equate to nothing (nil in objective-c speak). They are used in your code to help IB figure out what to do.

IBAction allows the UI to send your code an message when a button is pressed, and IBOutlet allows the UI to pull data directly from code if need be.

So if your UI in IB had a button, you could have it send an action to something indicating it was pressed. IB would list the method showName: only when it sees the IBAction property before it.

All of this is part of MVC (Model View Controller) design pattern. The idea being that your data is held in the Model, the UI components in the View and a object that marshals stuff back and forth in the Controller.

This way the model (how you represent data) can change willy nilly while the view continues to look the same while displaying varying data depending on the model.

You will learn a lot more about this when you get to Deletgates and things like NSListView as an example..
 
OP
S
Joined
Feb 14, 2013
Messages
4
Reaction score
0
Points
1
Thank you!

I guess I am confused because of my (little) knowledge of .Net
In .Net it suffices to update the labels text property using a method, as the showName method does. The extra steps of linking the two in the interface builder and declaring the label with the IBOutlet type are not necessary.

Guess i'll have to get the hang of it ;)
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top