Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Inside the iPhone: Getting Started : Page 4

Apple has lifted its NDA and developers are now free to talk about developing for the iPhone. In this first article of this "Inside the iPhone" series, you'll get familiar with the SDK by building a simple application and get a preliminary taste of what it's like to work with the mysterious Object-C.


advertisement
Controls Repositioning
Figure 10 showed you that as the orientation changes, the size and positioning of the controls remain. Ideally, your controls should reposition on the screen according to the view orientation.

Back in Interface Builder, select the Label control and select Tools→Size Inspector. Modify the Autosizing attribute of the control as shown in Figure 11. This will cause the Label control to expand/contract as the view orientation changes. At the same time, the control will anchor to the left, top, and right of the screen.

Likewise, modify the Autosizing attribute of the Text Field control.



Finally, modify the Autosizing attribute for the Round Rect Button control. Observe that, this time, you are not resizing the control when the view orientation changes. Instead, you are only going to anchor it to the top of the screen.


Figure 11. Autosizing: Modifying the attributes of the Label control for autosizing.
 
Figure 12. View Your Changes: Rotating the view within Interface Builder.

That's it! Within the Interface Builder, click on the arrow located at the top-right corner of the screen to rotate the screen to view your changes immediately (see Figure 12).

Go back to Xcode and run the application again. This time, notice that the controls will reposition and resize themselves as you rotate the screen.

Writing Some Code
So far, you haven't written any code. This was deliberate, because I wanted you to get comfortable with Xcode and the Interface Builder before embarking on coding. Nevertheless, let's write some code now to give you a taste of iPhone programming.

Recall that the Interface Builder contains a window labeled HelloDevXViewController.xib. Within this window, there are three components: File's Owner, First Responder, and View (see Figure 13). Select File's Owner, and then select Tools→Identity Inspector.

In the HelloDevXViewController Identity window, click the "+" button listed under Class Actions. Name the action btnClicked: (remember to include the colon). This creates an event handler named btnClicked:.


Figure 13. HelloDevXViewController.xib: Creating an event handler.
 
Figure 14. btnClicked:: Linking an event with an event handler.

Control-click the Round Rect Button control in the View window and drag it to the File's Owner item in the HelloDevXViewController.xib window (see Figure 14). A small pop-up containing the btnClicked: event will appear. Select the btnClicked: event. Basically, what you're doing here is linking the click event of the Round Rect Button control with the event handler.

In the HelloDevXViewController.h file, add in a header declaration for the btnClicked: event:

// // HelloDevXViewController.h // HelloDevX // // Created by Wei-Meng Lee on 11/12/08. // Copyright Developer Learning Solutions 2008. All rights reserved. // #import <UIKit/UIKit.h> @interface HelloDevXViewController : UIViewController { } //---declaration for the btnClicked: event--- -(IBAction) btnClicked:(id)sender; @end

In the HelloDevXViewController.m file, add in the code that provides the implementation for the btnClicked: event:

Figure 15. Your Alert Window: Tapping on a button displays an alert window.

- (void)dealloc { [super dealloc]; } //---implementation for the btnClicked: event--- -(IBAction) btnClicked:(id)sender { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello DevX" message: @"iPhone, here I come!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; [alert release]; } @end

The code above will display an alert window containing the sentence iPhone, here I come! Now, go back to Xcode and run the application again. This time, when you tap the Button control, an alert window will be displayed (see Figure 15).

Groundwork Laid
Though you haven't done much coding in this article, you're now familiar with Xcode and the Interface Builder, and can build a very simple iPhone application. Look for the subsequent articles in this series to get your hands dirty with the Objective-C language.



Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap