views:

405

answers:

5

Sorry I couldn't give a more descriptive title. My question (after looking at the code below) is what statusText is. Is it an IBOutlet or a UILabel? How does "@property (retain,nonatomic) UILabel *statusText" work? Does that statement mean that statusText is a property???

Thanks for answering. If there are any questions, please feel free to comment.

Button_FunViewController.h

@interface Button_Fun2ViewController : UIViewController {
    IBOutlet UILabel *statusText;
}
@property (retain,nonatomic) UILabel *statusText;
- (IBAction)buttonPressed: (id)sender;

Button_FunViewController.m

@synthesize statusText;

- (IBAction)buttonPressed: (id)sender {
    NSString *title = [sender titleForState:UIControlStateNormal];
    NSString *newText = [[NSString alloc] initWithFormat:@"%@ button was pressed.", title];
    statusText.text = newText;
    [newText.release]; 
}
+2  A: 

IBOutlet evaluates to nothing, it's just a hint to Interface Builder that it's a 'connection'.

And yes, statusText is a property then. There are three statements needed for a property (on a non-"modern runtime" system) – an ivar declaration, a @property statement, and a synthesize statement.

refulgentis
A: 

If you put IBOutlet in there like that, it will allow you to link the item while you're in interface builder.

Once you have the IBOutlet setup, you can open that class's nib and then select the File's Owner, then go to the inspector and drag a link from the Connections tab to the object.

This let's you then make changes to the object in your class's code and it will change the linked object. So once you link "statusText" to a specific UILabel, you can then use statusText.text = @"new text" in your class's code to modify the item, for example.

Also, IBAction allows you to make links in the same way. You can drag a connection from a control's event to the file owner and then select any method within that class that has IBAction as a return value.

mjdth
+2  A: 

statusText is an instance variable of type UILabel*. The IBOutlet keyword simply makes that instance variable available to Interface Builder.

@property declares accessor and/or mutator methods for the given property. It's equivalent to declaring -statusLabel and -setStatusLabel: methods.

You can use @synthesize to automatically implement these -statusLabel and -setStatusLabel: methods. The nonatomic and retain keywords define the behaviour of these automatically-generated methods.

Alternatively, you can implement the -statusLabel and -setStatusLabel: methods yourself.

Darren
yes but what purpose does the "@property" declaration do? also, how do I use the property?
Devoted
The purpose is to declare the publicly-accessible properties of your class. Typically, properties correspond to private instance variables, but they don't have to.
Darren
but in this example it does nothing i guess. cause i deleted the "@property" and "@synthesize" and it still worked.
Devoted
@koldfyre `@property` is simply a shorthand that will be expanded by the compiler to become accessor/mutator methods that adhere to the key value coding (KVC) standards. It's just a quick way of making sure you're KVC compliant since there's reduced opportunity for typographical errors - which are the bane of all Cocoa developers.
Tom Duckering
Properties aren't required for Interface Builder. That's where the IBOutlet keyword comes in.
Darren
A: 

There are in fact two statusText "things" in your example. There is a statusText object of type UILabel, and there is a statusText function created by @synthesize. When you use statusText from inside a method you are reffering to the object, not the function. Use self.statusText to use the property/function.

Tom Dalling
Technically, a method, not a function. /quibble
Jonathan Sterling
+2  A: 
  • what is statusText ?

statusText is a UILabel in your code example

  • Is it an IBOutlet or a UILabel?

Both.

UILabel is a type (a pointer to UILabel component that you use in GUI)

IBOutlet marks variable for Interface Builder application, so that it knows to show it as Outlet. During compilation IBOutlet is compiled out, it is defined in NSNibDeclarations.h as:

#define IBOutlet

  • How does @property (retain,nonatomic) UILabel *statusText work?

You can create accessors (getters/setters) for a variable by hand, no need to use property. You can just have UILabel *statusText and implement your getter/setters by hand.

You can have accessors declared by compiler by defining variable as a @property and then either use @synthesize to create accessors in .m file or again you declare the accessors yourself (you can override default accessors that would be generated)

You can have readwrite or readonly property - meaning either both setter and getter gets generated or only getter.

You can use copy, retain or assign for setter (see more about memory management about the tree optons copy/retain/assign)

There are some other options like nonatomic/atomic which has to do with generating mutexes and lock variable before access and so on (see more about properties)

For example if you have variable

NSString * string;

defining it as readwrite property and then synthesising you get the compiler to generate for you:

@property (copy, readwrite) NSString * string

then using

@synthesize string;

generates something like:

- (NSString *) string
{
    return string;
}

- (void)setString:(NSString *)str
{
    NSString * copy = [str copy];
    [string release];
    string = copy;
}
  • Does that statement mean that statusText is a property???

Yes you defined it as a property as explained above.

There are couple of concepts involved here. Definition of variable, defining it as IBOutlet for Interface Builder, declare variables as properties so that compiler generates getters/setters for you, defining type of getters/setters such as access method, memory management and locking.

I hope this explains your questions and if you follow the link you will find the explanation by Apple which I believe is quite clear about how to use properties.

Sorry for the horrible formatting ...

stefanB