http://www.cimgf.com/2008/10/01/cocoa-touch-tutorial-iphone-applica...
2. Enter Basic iPhone App as the project name. Click Save You should see a project workspace like the following:
1 de 5
12/04/2010 09:25
http://www.cimgf.com/2008/10/01/cocoa-touch-tutorial-iphone-applica...
The next thing you should do is create a class to act as your controller or delegate.
Delegate == Controller
The words delegate and controller can be used synonymously. Youll see later that we delegate the work of the different controls we create in Interface Builder to a delegate or controller class. In the iPhone template projects, this application delegate is created for you. Our app delegate has been named Basic_iPhone_AppAppDelegate. In our app delegate class we need to add what Cocoa developers refer to as outlets and actions. I could spend an entire post explaining these two things in depth, but for the sake of brevity and walking you through the steps to build your first application, the definition will have to suffice. Outlets represent controls in your user interface that can have some action performed upon them. Action are functions in your code that are connected to controls in your user interface such as a button or a drop down list. When connected to a button for instance, the action code will be run when the user clicks the button. In xcode, open your app delegate header file Basic_iPhone_AppAppDelegate.h. Add an outlet for the text field and the label below the window outlet as in the following snippet:
1 @interface Basic_iPhone_AppAppDelegate : NSObject <UIApplicationDelegate> { 2 IBOutlet UIWindow *window; 3 IBOutlet UITextField *textField; 4 IBOutlet UILabel *label; 5}
You will also want to add an action that will be performed when our button is clicked. Add that below the property for our window:
1 2 3 4 5 6 7 8 9 @interface Basic_iPhone_AppAppDelegate : NSObject <UIApplicationDelegate> { IBOutlet UIWindow *window; IBOutlet UITextField *textField; IBOutlet UILabel *label; } @property (nonatomic, retain) UIWindow *window; - (IBAction)click:(id)sender;
Now switch over to the implementation file, Basic_iPhone_AppAppDelegate.m. Add the click: action below our applicationDidFinishLaunching: function:
1 - (void)applicationDidFinishLaunching:(UIApplication *)application { 2 // Override point for customization after app launch 3 4 [window makeKeyAndVisible]; 2 de 5 12/04/2010 09:25
Cocoa Is My Girlfriend Cocoa Touch Tutorial: iPhone Application E... 4 5 6 7 8 9 10 [window makeKeyAndVisible]; } - (IBAction)click:(id)sender; { }
http://www.cimgf.com/2008/10/01/cocoa-touch-tutorial-iphone-applica...
We will actually add some code to do something in the click: action handler, but first we need to hook it up to the user interface in Interface Builder.
3 de 5
12/04/2010 09:25
http://www.cimgf.com/2008/10/01/cocoa-touch-tutorial-iphone-applica...
3. Control-Click and drag from the Button to your app delegate object in the MainWindow.xib window.
A pop-up will display. Select click: 4. Control-Click the app delegate object and drag it to the text field in the main window.
A pop-up will display. Select textField 5. Control-Click the app delegate object and drag it to the label in the main window.
4 de 5
12/04/2010 09:25
http://www.cimgf.com/2008/10/01/cocoa-touch-tutorial-iphone-applica...
A pop-up will display. Select label Thats it for Interface Builder. You can quit interface builder and return to xcode. We have one more piece of code to add and then our application will be finished.
Finishing Up
When the button is clicked, it will simply grab the text from the text field and place it into the label. Thats all the application does. Heres the code you need. Just make your implementation of the click: action in the Basic_iPhone_AppAppDelegate.m file look like this:
1 - (IBAction)click:(id)sender; 2{ 3 [label setText:[textField text]]; 4}
Notice we are grabbing the text from the text field, and setting the text in our label with it. Now all you need to do is click Build and Go. When the application runs the iPhone Simulator will be started. You will see the application load. Type some text into the text field and click the Change button. You will see the label update with the text from the text field.
Next Steps
This tutorial has just scratched the surface. Its is intended to get you going quickly. For a more in-depth path to gaining a deeper understanding of iPhone development, take a look at Apples, Your First iPhone Application. You will need to log in with your ADC account to see the article, but it is definitely worth while.
Conclusion
The limit with iPhone development is really just your imagination. It is an extremely fun platform to develop on and the resulting applications are very rewarding. Have fun with it and learn as much as you can. Just do me a favor and dont clog the App Store with any more flashlight apps or tip calculators Seriously ;-) . Until next time.
5 de 5
12/04/2010 09:25