Thursday, June 3, 2010

Start with Objective C

This is the post purely for the persons who are newly starting with Objective C.
Step 01: Create a New project in Xcode.




Step 02: Select a template from Xcode (let us make View-based Application)




Step 03: Give the Project Name




Step 04: The Solution could be opened with some files (.h, .m, .xib)




Step 05: Open Interface Builder by clicking .xib could lead you to View (Design) Page







Step 06: Open Library and (any) Inspector from Tool Bar





Step 07: Drag the component from Library to View.




Step 08: Let us do a count increment and count decrement program on click of buttons.
Make sure you are declaring Outlets in TestViewController.
Add outlets by clicking the ‘+’ symbol on the window (can do lblCount in Outlets).




Step 09: Make sure you are declaring Actions in TestViewController.
Add actions by clicking the ‘+’ symbol on the window (can do CountIncrement, CountDecrement in Actions).



(or)

Declare the Outlet Names in .h file (TestyViewController.h), instead of Step 8 and 9.
#import
@interface TestyViewController : UIViewController {
// Declare Outlets
IBOutlet UILabel *lblCount;
}
// Declare Actions
-(IBAction)CountIncrement: (id)sender;
-(IBAction)CountDecrement: (id)sender;
@end

Step 10: Connect the Outlets and Received Actions from Connections of Inspector with Components which are available in View Window.
To do it, just drag the mouse pointer from the empty ring to corresponding component.
Empty ring could be filled, after the connection made.






Step 11: Go to TestyViewController.m for coding the Action.


#implementation “TestyViewController.h”
@implementation TestyViewController
int countValue = 0;
-(IBAction)CountIncrement: (id)sender {
countValue++;
[lblCount setText: [NSString stringWithFormat:@”%d”, countValue]];
}
-(IBAction)CountDecrement: (id)sender {
countValue--;
[lblCount setText: [NSString stringWithFormat:@”%d”, countValue]];
}

Step 12: Build and Run it. Perform the buttons to verify.



1 comment: