Creating Hello World

So like every good tutorial we need to create a Hello World application. We will use what we have learned in the past tutorials and build on them.

So the first thing you want to do is open the file RootViewController.m

Look for the following code block

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return 0;
}

We want to make it return a 1 so make it look like the following

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return 1;
}

If we return 1 then it means we will have 1 row in the navigation view.

So now look for the following code block

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

	static NSString *MyIdentifier = @"MyIdentifier";

	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
	}

	// Set up the cell
	return cell;
}

Now we want to give the row some text. The one thing I really like about ojective C so far is how they handle getting and setting vars in a class.

So add [cell setText:@"Hello World"];

Here is the result

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

	static NSString *MyIdentifier = @"MyIdentifier";

	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
	}

	[cell setText:@"Hello World"];

	// Set up the cell
	return cell;
}

Now when we save the file and click Build and Go we should get the following.

So you have just completed the Hello World application. If you want to download the source grab it here. Helloworld.zip

I hope this helps!

About the default files

I will attempt to explain what the default files are. If I get these wrong please correct me.

So below is a screenshot of the default files that get placed in your project when you create a navigation based application.

  1. CoreGraphics.framework / Foundation.framework / UIKit.framework – These files are created by Apple that contain all the framework code we will be using to create the applications. You will never have to touch these but they should always be in your application.
  2. *.xib – These are files used by Interface Builder. I am not sure what they contain. If you double click them, Interface Builder will start
  3. *.h – These are header files. They are pretty much the same as all header files like c/c++
  4. *.m – These are the meat of the application. They are like .c or .cpp files
  5. *.app – This is the application file that will go on the Iphone.

Setup your development enviroment

I am assuming that you have the Iphone SDK installed. If you need help with getting that installed or do not know what a SDK is then this blog will probably be over your head.

So first thing is to start Xcode. I have mind in the menu bar (sorry I am not a Mac person so I have no clue what it is called )

Once Xcode is started you will be greated with the opening screen that looks like this.

Once you are on the following screen click on File -> New Project and you will see the following screen

Make sure you are selected on the Iphone OS and Application option in the left hand panel. Also you want to Select the Navigation Based Application. Once that is done click Choose and then give a name to your project that has some meaning.

Once you do that you will see the following screen that has all the source code and build files in it.

From here you can click on the Build and Go icon at the top and it should compile correctly and launch into the Iphone Simulator where it will be a blank navigation.

There it is you have successfully created a very simple non-functional Iphone application.

Welcome!

Welcome to the new blog. Let me give you a little background of what I want to do with this blog. I want to create a blog that teaches people with some programming knowledge on how to program objective C for the Iphone.

I come from a mostly PHP, Pyhton and C++ (Qt widget set) background. I am also the lead developer for Photoblog and was looking to create a Iphone app to tie into the site. As of writing this I know almost nothing about objective C. I just looked at it for around 3 hours the day before.

So come learn objective C with me and hopefully we can learn from each other.

← Previous Page