the first program. step 1 file->new project create a project named “helloworld”, and select...

11
IPHONE The First Program

Upload: linette-watts

Post on 17-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

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

IPHONEThe First Program

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

Step 1

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

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

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.

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

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.

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

#import <UIKit/UIKit.h>@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

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

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;

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

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 .}

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

   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;    

 

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

//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];

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

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:@""];}

}

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

Step 5

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