HelloWorld Example in Xcode 4 – iPhone OS 4.3

来源:互联网 发布:国产男装品牌 知乎 编辑:程序博客网 时间:2024/05/17 21:59

This is the “Hello World” example. I am going to execute the Hello World example in a new version of Xcode. So many things are new in this Xcode 4.

Xcode 4 is the complete toolset for building Mac OS X and iOS applications, the tools have been redesigned to be faster, easier to use, and more helpful than ever before. The Xcode IDE understands your project’s every detail, identifies mistakes in both syntax and logic, and will even fix your code for you. Quite simply, Xcode 4 will help you write better code.

So let see how it will be work.

Step 1: Open a Xcode, select Create a new Xcode project (See fig 1) and click open button.

Figure 1

Now select ViewBase application and click next (See figure 2)

Figure 2

Next page you need to define Product name and Identifier name, the identifier is the name of your application. It’s the same name used when you register your application at Apple’s website and release it to the AppStore. The Product name “HelloWorld Xcode4” and the Identifier name “com.mycompany.APPLICATIONNAME” (See figure 3). Select the Device Family into iPhone. Now select next and save the application in your disk.

Figure 3

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory (See the figure 4).

Figure 4

Step 3: Expand classes and notice Interface Builder created the HelloWorld_Xcode4ViewController class for you and generated a separate nib, HelloWorld_Xcode4ViewController.xib, for the “HelloWorld_Xcode4”.

Step 4: Select the HelloWorld_Xcode4ViewController.h file from the Project Navigator. We have added UILabel for display the text and added IBAction buttonPressed; in the file, so make the following changes in the file.

#import <UIKit/UIKit.h>

@interface HelloWorld_Xcode4ViewController : UIViewController {
  
    IBOutlet UILabel *label;
        
}
@property (nonatomic, retain) IBOutlet UILabel *label;

-(IBAction)buttonPressed;

@end

Step 5: Double click the HelloWorld_Xcode4ViewController.xib file from Project Navigator. Now the .xib window will look like (figure 5).

Figure 5

a) Now drag the label and RoundRect button from the object (See figure 6).

Figure 6

b) Select Label from the Window and change the label Text name from the Attribute Inspector (See figure 7). Give the Text name “Hello World!!!”.

Figure 7

c) Next select the RoundRect Button from the window and select Attribute Inspector (See figure 8). Give the Title “Click Me”.

Figure 8

d) Now connect File’s Owner icon to the “Hello World!!!” label and select label (See figure 9).

Figure 9

e) Select “Click Me” button and bring up Connection Inspector (See figure 10) and connect Touch up Inside to the File’s Owner icon and select buttonPressed: method.

Figure 10

f) Select File’s Owner icon to the View window and select view (See figure 11).

Figure 11

Now save it and open the HelloWorld_Xcode4ViewController.m file from the Project Navigator.

Step 6: In the HelloWorld_Xcode4ViewController.m file make the following changes.

#import "HelloWorld_Xcode4ViewController.h"

@implementation HelloWorld_Xcode4ViewController

@synthesize label;

-(IBAction)buttonPressed{
        if ([label.text isEqualToString:@"Hello World!!!"])
                label.text = @"Hello iPhone!!!";
        else 
                label.text = @"Hello World!!!";
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn’t have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren’t in use.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

Step 7: Now Compile and Run the application and see the Output in the Simulator.

原创粉丝点击