Presentation is loading. Please wait.

Presentation is loading. Please wait.

The First Program. Step 1  File->New Project Create a project named “HelloWorld”, and select View Based Application from the icons on the right.

Similar presentations


Presentation on theme: "The First Program. Step 1  File->New Project Create a project named “HelloWorld”, and select View Based Application from the icons on the right."— Presentation transcript:

1 The First Program

2 Step 1  File->New Project Create a project named “HelloWorld”, and select View Based Application from the icons on the right.

3 Step 1  There are four files in the Classes package.  In the “HelloWorldAppDelegate.m” we find an auto generated applicationDidFinishLaunching method. This, as the name suggests, invoked when the application has been loaded and in this method we will add our HelloWorldViewController object to the UIWindow and make it visible.

4 Step 2  Open the HelloWorldViewController.h file  We are defining a view to display and a button and label to go in the view. After the curly braces add a method declaration to accept the click event of the button and also properties to access the UI elements; myButton, myLabel and myView.

5  #import @interface HelloWorldViewController : UIViewController { IBOutlet UIButton *button; IBOutlet UILabel *label; IBOutlet UIView *myView; } -(IBAction)handleEvent:(id)sender; @property (nonatomic, retain) UIButton *button; @property (nonatomic, retain) UILabel *label; @property (nonatomic, retain) UIView *myView; @end

6 Step 3  add a synthesize the three UI elements to create the getter and setters. Add the following three lines of code after the @implementation HelloWorldViewController command of HelloWorldViewController.m file. @synthesize button; @synthesize label; @synthesize myView;

7 Step 3  Now find the comment saying "Implement loadView if you want to create a view hierarchy programmatically." and un-comment the loadView method that follows this line. - (void)loadView { // Add the following lines to the method to create a button and label. }

8 CGRect cgRct = CGRectMake(0,0, 480, 320); //define size and position of view myView = [[UIView alloc] initWithFrame:cgRct]; //initialize the view myView.autoresizesSubviews = YES; //allow it to tweak size of elements in view self.view = myView; // create a UIButton and play arround with settings button = [UIButton buttonWithType: UIButtonTypeRoundedRect]; button.frame = CGRectMake(100, 100, 100, 50); [button setTitle:@"Add" forState:UIControlStateNormal]; button.backgroundColor = [UIColor clearColor]; button.adjustsImageWhenHighlighted = YES;

9 //Add action handler and set current class as target [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside]; //Display Button [self.view addSubview:button]; //create a label cgRct = CGRectMake(100, 170, 100, 50); //define size and position of label label = [[UILabel alloc] initWithFrame:cgRct]; label.text = @"Hello World"; //Display Label [self.view addSubview:label];

10 Step 4  Finally, we have to define the button to send an event (action) to the current class (self) in case of an UIControlEventTouchUpInside event. -(void)action:(id)sender { if([[label text] length]==0) { [label setText:@"Hello World!"]; } else { [label setText:@""]; } }

11 Step 5  Click the Build and Go button to check your interface is drawn correctly It should look something like to screen shot below.


Download ppt "The First Program. Step 1  File->New Project Create a project named “HelloWorld”, and select View Based Application from the icons on the right."

Similar presentations


Ads by Google