Create a Hello World tutorial using Interface Builder

Source: Internet
Author: User

Interface BuilderThis document describes how to create a Hello World tutorial.IPhoneSDK Beta 2 andInterface BuilderMake the iPhone Hello World. Let's just look at the details first.

Apple has released the latest iPhone SDK Beta 2 for us, along with a super Interface Builder. Apple fans will not give up without doing anything. However, Apple is not so honest that there is not much to mention how to combine Interface Builder and iPhone SDK.

This article will teach you how to make a simple Hello World. This tutorial is different from any official website you can see. Similarly, HelloWorldClassic included in the iPhone example is totally different. Because it uses Interface Builder to create the iPhone UI, rather than using a lot of code to create buttons, input boxes, and so on.

In this tutorial, you will find that the process of developing a simple iPhone application with the IB + iPhone SDK is almost the same as that of developing a Mac OS program. In this example, we use the xib file instead of nib to create the interface.

This Hello World does not do much. There is only one button and one Label on the screen. Press the button and the Label will display Hello World! That's simple. And you will also find that the code is extremely simple, completely different from the examples of other iPhone sdks you see. This is the miracle brought by the latest Interface Builder. This tutorial assumes that you have a little experience in Mac development, but I am worried that you may not understand this article. If you don't understand it, it doesn't matter. Let's take a look at the other basic articles developed by Apple on this site, and then come back to this article.

The show started!

First, you need to create a project. I believe that taking a picture here will show you how to build an iPhone project is insulting your IQ, because when you download a 2G SDK package, the first thing you need to do is this. So let's go to the more important thing: Modify the default code.

After the default project is created, it will create several files for you and does not add the xib file. I believe it will be automatically added to the SDK 2.0 official version. The default AppDelegate. m looks like this:

 
 
  1. self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];  
  2. // Set up content view  
  3. self.contentView = [[[MyView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];  
  4. [window addSubview:contentView];  
  5. // Show window  
  6. [window makeKeyAndVisible]; 

At the same time, the system will automatically create a MyView for you. The first thing we need to do is to get rid of this MyView, because with it, you can only write a lot of code for creating buttons and text boxes to implement the functions we want. The thing you need to do is to delete the above pile and replace it with the following line:

 
 
  1. self.window = [[[NSBundle mainBundle] loadNibNamed:@"MainWindow" owner:self options:nil] objectAtIndex:0];  
  2. [window makeKeyAndVisible]; 

Drag a button and a Label.

Open Interface Builder and create New), and select create a Cocoa Touch-> Window.

Drag a button and a Label.

Then press command + S to save it. Remember to save it to the project directory you just created and name it MainWindow.

Interface Builder will ask you if you want to add it to the project. Of course, check and select Add ).

Build and Run, Run it, and you will see your window and the button and text box you just threw in the iPhone simulator.

Next, let's make some fun work. Press the button and the text box will change to Hello World!

Return to Interface Builder, select the window you just selected, and open Inspector. in the Class, the default value is UIWindow. Change it to the name you want, for example, MyUIWin.

Add a method in Class Actions, such as hello: Don't forget the colon. Because the Objective-C method must contain colons. In the following Class Outlet, click the plus sign to add an outlet to indicate our Label, for example, showhello.

After completing these two tasks, we need to add these tasks to the project. Select your MyUIWin window, and select Write Class... Save and add it to the project.

Return to XCode and change the comments behind @ interface MyUIWin: To UIWindow in MyUIWin. h and add a line in OOXXAppDeletegate. h:

Press Command + S to save, and we will return to Interface Builder.

Press and hold the control key. You can drag a line from MyUI Win to the Label. Is it similar to the Mac Cocoa program? Drag it up and select showhello, which is the outlet. Next, select the button. In Inspector's Events, you will see a message called Touch Down, holding the circle behind it and dragging it to MyUI Win. Jump out of a hello method and select it.

Save. We have finished our work. Build and Run. Press the button and you will find that Hello World is displayed. If the problem persists, go back and read the article again to see what you did wrong.

If you know how to use XCode to write Mac programs, you will find that using the Interface Builder provided by iPhone SDK Beta2 to develop iPhone programs is almost the same as writing desktop programs. If you read this article and find that you do not need to explain it, and you have already taken the photo, congratulations! developing the iPhone is that simple.

 
 
  1. @class MyUIWin; 

Then

 
 
  1. UIWindow *window; 

And

 
 
  1. @property (nonatomic, retain) UIWindow *window; 

Change

 
 
  1. MyUIWin *window; 

And

 
 
  1. @property (nonatomic, retain) MyUIWin *window; 

Return to MyUIWin. h,

Change # import <Cocoa/Cocoa. h> to # import <UIKit/UIKit. h>, and then change IBOutlet id showhello to IBOutlet UILabel * showhello;

Start MyUIWin. m and find

 
 
  1. - (IBAction)hello:(id)sender { 

Add a line in it:

 
 
  1. [showhello setText:@"Hello World!"]; 

Download this example to: http://www.mobpub.net/viewthread.php? Tid = 3335 & page = 1 & extra = # pid3585

Well, don't run it. We need a MainWindow.

Summary:Interface BuilderI hope this article will help you understand the content of the Hello World tutorial!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.