Developing for iPhone

Learn how to create and develop iPhone applications from scratch.

Archive for the ‘Objective-C’ Category

Tutorial 3-3: More Interface Connections

Posted by Henry On May - 2 - 2009 3 COMMENTS

Go ahead and open the “HelloiPhone” project from the previous tutorial. Remember the slider we added to the interface? Today we will give it some functionality.
Open “HelloiPhoneViewController.h” and edit it to look like this:

#import <uikit /UIKit.h>
 
@interface HelloiPhoneViewController : UIViewController {
	IBOutlet UILabel *sliderValue;
}
 
@property (nonatomic, retain) UILabel *sliderValue;
 
-(IBAction) sliderChanged: (id) s;
-(IBAction) goButton;
 
@end
 
</uikit>

Read the rest of this entry »

Tutorial 3-2: Interface Connections

Posted by Henry On May - 1 - 2009 1 COMMENT

Open the “HelloiPhone” project we worked on in the previous tutorial.

Today we will start by adding a button to our interface. So click the Resources folder in the project window and double click “HelloiPhoneViewController.xib” to open the Interface Builder. We already have a label set up, so let’s add a button below it. In Interface Builder, select “Round Rect Button” in the Library window and drag it onto your view just below the label we created. Double click on your new button and type in “GO”. Feel free to move or resize the button however you want. You can resize using the blue dots when the button is selected. When you are happy with your button, proceed to the next step.

We have a button now, but it doesn’t do anything yet. Let’s add a slider and then we can add functionality to them later. Drag a “Silder” from the Library window to your view somewhere below the button. Resize it to fill the width of the view. Now drag another “Label” from the Library and place it below the slider. This label will change when the slider is moved with the help of some code. Change the label to say “0.5″. When the slider is to the left, we will set the slider label to 0 and set it to 1 when it is to the right. Since we are initializing the slider in the middle, the label should initially read 0.5. Highlight the new label and set the alignment to the center. This can be done in the Attributes Inspector. Your interface should now look something like this:

Read the rest of this entry »

Tutorial 2-4: Inheritance

Posted by Henry On April - 23 - 2009 1 COMMENT

Another reason object-oriented programming languages are so powerful is inheritance. Inheritance lets a class inherit methods and variables from another class. To demonstrate this, let’s make a new class that will inherit the Television class we already created. Open your “Objects Intro” project and click File > New File. Select Cocoa from the left pane and Objective-c Class from the right pane. Click next and name it “MutableTelevision.m” (make sure “create .h” box is ticked) then click Finish.

Ok, now we have two classes in our project. “Television” and “MutableTelevision”. The Television class does the basic things that we would expect a television to do. But what if we want a class that does all that but has extra features? We could write all the methods again for the new class, but an easier option would be to use inheritance and make MutableTelevision a child of Television.

Open “MutableTelevision.h” and let’s start creating the interface file. Edit it to look like this:

#import <cocoa /Cocoa.h>
#import "Television.h"
 
@interface MutableTelevision : Television
 
-(void) mute;
 
@end
 
</cocoa>

Read the rest of this entry »

Tutorial 2-3: The if Statement

Posted by Henry On April - 22 - 2009 1 COMMENT

Let’s start by opening the “Objects Intro” project from the last tutorial. If you haven’t done so already, I strongly recommend that you add some comments to the code you have written so far which explain what each part does.

Once you have done that, open “Objects Intro.m” and edit it to look like this:

#import <foundation /Foundation.h>
#import "Television.h"
 
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
	int chanChange;
 
	Television *myTV = [[Television alloc] init];
 
	NSLog(@"Type in the channel number:");
	scanf("%i", &chanChange);
 
	[myTV changeChannel: chanChange];
	[myTV watch];
 
    [myTV release];
    [pool drain];
    return 0;
}
</foundation>

Read the rest of this entry »

Tutorial 2-2: Properties and Accessor Methods

Posted by Henry On April - 20 - 2009 1 COMMENT

In this tutorial, we will need to use the project that we started in the last tutorial. So go ahead and open your “Objects Intro” project.

If you remember, last time we created the Television class which we used to create a Television object which could print out two different statements. Today we will add a new method that will change the channel of the television. To do this the television needs to be able to store some data. Let’s go ahead and create a space to store that data now. Open “Television.h” and edit the empty brackets to look like this:

@interface Television : NSObject {
	int channel;
}

We have now got an instance variable in which to store the channel number. An instance variable is data held by an object. For example, if we created two Television objects from the Television class, we could set the channel number of myTV1 to 3, and the channel number of myTV2 to 7. Both Televisions are produced by the Television class, but each can store their own data. But how do we change the channel? This is where Accessor Methods come in.

Read the rest of this entry »

I know you are probably itching to get stuck into the iPhone programming environment, but there are a few things you HAVE to learn first. One of them is Object Oriented Programming. The reason I am jumping right into this is that it is the main feature of Objective-C and a very powerful set of tools which you will need to understand if you want to create iPhone apps.

Let’s start by creating a new project in XCode. Fire up XCode and follow these steps:

  1. In XCode, click File > New Project
  2. Choose Command Line Utility > Foundation Tool
  3. Name the project “Objects Intro” and save it.

In the project window, click the Source folder in the left-hand pane to view your source files. You should have one called “Objects Intro.m” and one called “Objects Intro_Prefix.pch” (which you can ignore for now.)

Object oriented programming languages are based around the use of objects. Objects can represent real life objects (like a chair or a car), or can be more abstract. Objects can hold variables, change variables and pass information between each other. This makes the development of complex applications much more simple. You will understand them more as we continue. Let’s make an object right now!
 

Read the rest of this entry »

Tutorial 1-4: More Variables

Posted by Henry On April - 14 - 2009 1 COMMENT

In this tutorial, I will be showing you a couple more ways to use variables.

Open your “Hello World” project from the previous tutorials, and open the “Hello World.m” file to start editing. Edit the program so it looks like this:

#import <foundation /Foundation.h>
 
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
	int age;
	int fingers;
 
	age = 99;
	fingers = 10;
 
	NSLog(@"I am %i years old and I have %i fingers!", age, fingers);
 
    [pool drain];
    return 0;
}</foundation>

Read the rest of this entry »

Tutorial 1-3: Introduction to Variables

Posted by Henry On April - 13 - 2009 1 COMMENT

This tutorial assumes you have followed tutorials 1-1 and 1-2, if you haven’t – go and complete these now.

Ok, let’s open our project we made in the previous tutorial. To open a project follow these steps:

  1. Start XCode
  2. Click File > Open then navigate to your project folder. This will be in the directory you saved it to.
  3. In your project folder, click “Hello World.xcodeproj” and click the Open button.
  4. Your project should open in a new window. Another way of opening a project is by navigating to it in finder and double-clicking the .xcodeproject file. If you have had the project open recently, click File > Open Recent Project and look for the project you want in the list.

Now you should have your project open. Double-click “Hello World.m” to open it for editing. Feel free to run it again to remind yourself what the program does. Remember, you can do this by clicking the Build and Go icon.

 

Read the rest of this entry »