To add a property to a UIViewController, you first need to declare it in the UIViewController’s header file.
Here’s an example:
|
// // RootViewController.h // App Name // // Created by Miscellanea on 11/03/11. // Copyright 2011 __MyCompanyName__. All rights reserved. // #import <UIKit/UIKit.h> @interface RootViewController : UIViewController @property (nonatomic, retain) NSMutableArray *items; @end |
Note that primitives (like int and float) don’t need the retain attribute in the @property statement.
Once a property has been declared in the header file, it can be defined in the corresponding implementation (.m) file:
|
// // RootViewController.m // App Name // // Created by Miscellanea on 11/03/11. // Copyright 2011 __MyCompanyName__. All rights reserved. // #import "RootViewController.h" @implementation RootViewController @synthesize items, index; - (void)dealloc [self.items release]; - (void)viewDidUnload self.items = nil; @end |
You first need to synthesize the property at the top of the file. If you declared the property with the retain attribute in the header, it will also need to be released in the dealloc method, and generally set to nil in the viewDidUnload method as well.
Primitive properties (like int and float) only need to be synthesized.


1 comment
Comments feed for this article
November 10, 2011 at 4:06 pm
How to Access Custom Properties in Interface Builder « Miscellanea
[...] easy to add a property to a UIViewController, but how do you access it from Interface Builder when you’re [...]