How to Add Outlets to a Modal View Controller in Your iPad App

Before using Interface Builder to create the elements for the Modal view of your iPad app, you should first put outlets in your app's code that will connect your methods to the Interface Builder interface objects. You need to do three things in your code to have the compiler create accessors for you:




  1. Declare an instance variable in the interface file.




  2. Add an @property declaration of that instance variable in the same interface file (usually with the nonatomic attribute).


    The declaration specifies the name and type of the property, as well as some attributes that provide the compiler with information about how exactly you want the accessor methods to be implemented.


    For example, the declaration @property (nonatomic, assign) NSString* wordsOfWisdom; declares a property named wordsOfWisdom, which is a pointer to an NSString object. As for the two attributes — nonatomic and assignnonatomic tells the compiler to create an accessor to return the value directly, which is another way of saying that the accessors can be interrupted while in use. The second value, assign, tells the compiler to create an accessor method that sends an assign message to any object that's assigned to this property.




  3. Use @synthesize in the implementation file so that Objective-C generates the accessors for you.


    The @property declaration only declares that there should be accessors. It's the @synthesize statement that tells the compiler to create them for you.




In the following example code, you need to create two outlets: one to point to a text entry field and one to point to a speed slider. To get this outlet business started, you need to declare each outlet, which you do with the help of the IBOutlet keyword.


#import <UIKit/UIKit.h>
@protocol SettingsViewControllerDelegate;
@interface SettingsViewController : UIViewController {
<SettingsViewControllerDelegate> delegate;
NSString *wordsOfWisdom;
float sliderValue;
IBOutlet UITextField *theTextField;
IBOutlet UISlider *slider;
}
- (IBAction) done;
- (IBAction) speedChanged: (id) sender;
@property (nonatomic, assign) id <SettingsViewControllerDelegate> delegate;
@property (nonatomic, assign) NSString* wordsOfWisdom;
@property (nonatomic, assign) UISlider* slider;
@end
@protocol SettingsViewControllerDelegate
- (void) settingsViewControllerDidFinish:(SettingsViewController *)controller;
- (void) changeSpeed: (double) newSpeed;
@end

Two action methods (done and speedChanged) for Interface Builder elements are declared (with IBAction), along with the IBOutlet statements, which declare the outlets that will be automatically initialized with a pointer to the UITextField (theTextField) and the UISlider (slider) when the application is launched. But while this will happen automatically, it won't automatically happen automatically. You have to help it out a bit.


The methods that provide access to the instance variables of an object are called accessor methods, and they effectively get (using a getter method) and set (using a setter method) the values for an instance variable. Although you can code those methods yourself, it can be rather tedious. This is where properties come in. The Objective-C Declared Properties feature provides a simple way to declare and implement an object's accessor methods. The compiler can synthesize accessor methods according to the way you told it to in the property declaration.










dummies

Source:http://www.dummies.com/how-to/content/how-to-add-outlets-to-a-modal-view-controller-in-y.html

No comments:

Post a Comment