In this tutorial we will see  how to view application dynamically. As of now you have been created application visually using Interface Builder. It makes relatively easy to build a UI using drag and drop, sometimes you are better off using code to create it. In the following Try It Out, you learn how to create views dynamically form code.

TRY IT OUT Creating Views from Code


1. Using Xcode, create a View-based Application project and name it DynamicViews.

2. In the DynamicViewsViewController.m fi le, add the following statements that appear in bold:

#import “DynamicViewsViewController.h”
@implementation DynamicViewsViewController
- (void)loadView {
//---create a UIView object---
UIView *view =
[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
//---set the background color to light gray---
view.backgroundColor = [UIColor lightGrayColor];
//---create a Label view---
CGRect frame = CGRectMake(10, 15, 300, 20);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:@”Verdana” size:20];
label.text = @”This is a label”;
label.tag = 1000;
//---create a Button view---
frame = CGRectMake(10, 70, 300, 50);
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
[button setTitle:@”Click Me, Please!” forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
button.tag = 2000;
[button addTarget:self
action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
[view addSubview:label];
[view addSubview:button];
self.view = view;
[label release];
}
-(IBAction) buttonClicked: (id) sender{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@”Action invoked!”
message:@”Button clicked!”
delegate:self
cancelButtonTitle:@”OK”
otherButtonTitles:nil];
[alert show];
[alert release];
}


3. Press Command-R to test the application in the iPhone 4 Simulator. Figure 3-20 shows that the Label and Round Rect Button views are displayed on the View window. Click the button to see an alert view displaying a message.

How It Works


You implement the loadView method defi ned in your View Controller to programmatically create your
views. You implement this method only if you are generating your UI during runtime. This method is
automatically called when the view property of your View Controller is called but its current value is nil.

The fi rst view you create is the UIView object, which allows you to use it as a container for more views:

//---create a UIView object---
UIView *view =
[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
//---set the background color to light gray---
view.backgroundColor = [UIColor lightGrayColor];

Next, you create a Label view and set it to display a string:
//---create a Label view---
CGRect frame = CGRectMake(10, 15, 300, 20);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:@”Verdana” size:20];
label.text = @”This is a label”;
label.tag = 1000;

Notice that you have also set the tag property, which is very useful for allowing you to search for particular
views during runtime.
You also create a Button view by calling the buttonWithType: method with the UIButtonTypeRoundedRect
constant. This method returns a UIRoundedRectButton object (which is a subclass of UIButton).
//---create a Button view---
frame = CGRectMake(10, 70, 300, 50);
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
[button setTitle:@”Click Me, Please!” forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
button.tag = 2000;

You then wire an event handler for its Touch Up Inside event so that when the button is tapped, the
buttonClicked: method is called:
[button addTarget:self
action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];

Finally, you add the label and button views to the view you created earlier:
[view addSubview:label];
[view addSubview:button];

Finally, you assign the view object to the view property of the current View Controller:
self.view = view;
Download Code

In this tutorial we will see how view webpage in our application. If we want to load web pages from within our application, we must embed a web browser in our application through the use of the UIWebView. Using Web view, we can send a request to load web content. The following Try It Out shows how to use the Web view to load a Web page.


TRY IT OUT  Loading a Web Page Using the Web View


1 Using Xcode, create a new View-based Application project and name it UsingViews2.

2 Double-click the UsingViews2ViewController.xib fi le to edit it using Interface Builder.

3 In the View window, add a Web View from the Library. In the Attributes Inspector window for the Web view, check the Scales Page to Fit property.

4 In the UsingViews2ViewController.h fi le, declare an outlet for the Web view:

#import 
@interface UsingViews2ViewController : UIViewController {
IBOutlet UIWebView *webView;
}
@property (nonatomic, retain) UIWebView *webView;
@end

5 In Interface Builder, connect the webView outlet to the Web view.



6. In the UsingViews2ViewController.m file, add the following statements that appear in bold:
#import “UsingViews2ViewController.h”
@implementation UsingViews2ViewController
@synthesize webView;
- (void)viewDidLoad {
NSURL *url = [NSURL URLWithString:@”http://www.apple.com”];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];
[super viewDidLoad];
}
- (void)dealloc {
[webView release];
[super dealloc];
}

7. Press Command-R to test the application on the iPhone 4 Simulator. You should see the application
loading the page from Apple.com.



How It Works


To load the Web view with a URL, you first instantiate an NSURL object with a URL via the URLWithString method:
NSURL *url = [NSURL URLWithString:@”http://www.apple.com”];

You then create an NSURLRequest object by passing the NSURL object to its requestWithURL: method:
NSURLRequest *req = [NSURLRequest requestWithURL:url];

Finally, you load the Web view with the NSURLRequest object via the loadRequest: method:
[webView loadRequest:req];

Download Code

In this tutorial we will see how to make a alert box in iphone. This alert box is viewed by user and is usually created during runtime. This alertview is useful for cases in which you have to display a message to the user. Try It Out explores the UIAlertView in more detail. Download the code as indicated.


TRY IT OUT Using Alert View


1 Using Xcode, create a new View-based Application (iPhone) project and name it UsingViews.

2 In the UsingViewsViewController.m fi le, add the following bold code to the viewDidLoad method:

- (void)viewDidLoad {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@”Hello”
message:@”This is an alert view”
delegate:self
cancelButtonTitle:@”OK”
otherButtonTitles:nil];
[alert show];
[alert release];
[super viewDidLoad];
}

3 Press Command-R to test the application on the iPhone 4 Simulator. When the application is loaded, you see the alert view.

 4 In Xcode, modify the otherButtonTitles parameter by setting it with the value shown in bold:
viewDidLoad {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@”Hello”
message:@”This is an alert view”
delegate:self
cancelButtonTitle:@”OK”
otherButtonTitles:@”Option 1”, @”Option 2”, nil];
[alert show];
[alert release];
[super viewDidLoad];

5 In the UsingViewsViewController.h fi le, add the following line that appears in bold:
#import 
@interface UsingViewsViewController : UIViewController
 {
}
@end



6. In the UsingViewsViewController.m file, add the following method:
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@”%d”, buttonIndex);
}


7. Press Command-R to test the application in the iPhone 4 Simulator. Notice that there are now two buttons in addition to the OK button.


8. Click any one of the buttons — Option 1, Option 2, or OK.

9. In Xcode, press Command-Shift-R to view the Debugger Console window. Observe the values printed. You can rerun the application a number of times, clicking the different buttons to observe
the values printed. The values printed for each button clicked are as follows:

➤➤ OK button — 0
➤➤ Option 1 — 1
➤➤ Option 2 — 2
Download Code



In this tutorial we will see how make a outlet and action in iphone.  Before we are going to this you need to understand in iPhone programming is outlets and actions. If you are familiar with traditional programming languages such as Java or C#, this is a concept that requires some time to get used to the concepts are similar, just that it is a different way of doing things.


1. Using Xcode, create a View-based Application (iPhone) project and name it OutletsAndActions.

2. Edit the OutletsAndActionsViewController.xib file by double-clicking it to open it in Interface Builder. When Interface Builder is loaded, double-click the View item in the OutletsAndActionsViewController.xib window to visually display the View window. Populate the three views onto the View window — Label, Text Field, and Round Rect Button. Set the Label view with the text “Enter your name” by double-clicking on it. Also, set the Round Rect Button with the “OK” string.


3. In Xcode, modify the OutletsAndActionsViewController.h file with the following statements:

#import <UIKit/UIKit.h>
@interface OutletsAndActionsViewController : UIViewController 
{
//---declaring the outlet---
IBOutlet UITextField *txtName;
}
//---expose the outlet as a property---
@property (nonatomic, retain) UITextField *txtName;
//---declaring the action---
-(IBAction) btnClicked:(id) sender;
@end;

4. In the OutletsAndActionsViewController.m file, define the following statements:

#import “OutletsAndActionsViewController.h”
@implementation OutletsAndActionsViewController
//---synthesize the property---
@synthesize txtName;
//---displays an alert view when the button is clicked---
-(IBAction) btnClicked:(id) sender {
NSString *str = [[NSString alloc]
initWithFormat:@”Hello, %@”, txtName.text];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@”Hello!”
message:str
delegate:self
cancelButtonTitle:@”Done”
otherButtonTitles:nil];
[alert show];
[str release];
[alert release];
}
- (void)dealloc {
//---release the outlet---
[txtName release];
[super dealloc];
}

5. In the OutletsAndActionsViewController.xib window, control-click and drag the File’s Owner item to the Text Field view. A popup will appear; select the outlet named txtName.

6. Control-click and drag the OK Button view to the File’s Owner item. Select the action named btnClicked:.

7. Right-click the OK Button view to display its events. Notice that the Button view has several events, but one particular event — Touch Up Inside — is now connected to the action you specified (btnClicked:). Because the Touch Up Inside event is so commonly used, it is automatically connected to the action when you control-click and drag it to the File’s Owner item. To connect other events to the action, simply click the circle displayed next to each event and then drag it to the File’s Owner item.




8. That’s it! Press Command-R to test the application on the iPhone 4 Simulator. Enter a name in the Text Field and click the OK button. An alert view displays a welcome message.





Download Code


In this tutorial we will see  how to view application dynamically. As of now you have been created application visually using Interface Builder. It makes relatively easy to build a UI using drag and drop, sometimes you are better off using code to create it. In the following Try It Out, you learn how to create views dynamically form code.

TRY IT OUT Creating Views from Code


1. Using Xcode, create a View-based Application project and name it DynamicViews.

2. In the DynamicViewsViewController.m fi le, add the following statements that appear in bold:

#import “DynamicViewsViewController.h”
@implementation DynamicViewsViewController
- (void)loadView {
//---create a UIView object---
UIView *view =
[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
//---set the background color to light gray---
view.backgroundColor = [UIColor lightGrayColor];
//---create a Label view---
CGRect frame = CGRectMake(10, 15, 300, 20);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:@”Verdana” size:20];
label.text = @”This is a label”;
label.tag = 1000;
//---create a Button view---
frame = CGRectMake(10, 70, 300, 50);
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
[button setTitle:@”Click Me, Please!” forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
button.tag = 2000;
[button addTarget:self
action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
[view addSubview:label];
[view addSubview:button];
self.view = view;
[label release];
}
-(IBAction) buttonClicked: (id) sender{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@”Action invoked!”
message:@”Button clicked!”
delegate:self
cancelButtonTitle:@”OK”
otherButtonTitles:nil];
[alert show];
[alert release];
}


3. Press Command-R to test the application in the iPhone 4 Simulator. Figure 3-20 shows that the Label and Round Rect Button views are displayed on the View window. Click the button to see an alert view displaying a message.

How It Works


You implement the loadView method defi ned in your View Controller to programmatically create your
views. You implement this method only if you are generating your UI during runtime. This method is
automatically called when the view property of your View Controller is called but its current value is nil.

The fi rst view you create is the UIView object, which allows you to use it as a container for more views:

//---create a UIView object---
UIView *view =
[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
//---set the background color to light gray---
view.backgroundColor = [UIColor lightGrayColor];

Next, you create a Label view and set it to display a string:
//---create a Label view---
CGRect frame = CGRectMake(10, 15, 300, 20);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:@”Verdana” size:20];
label.text = @”This is a label”;
label.tag = 1000;

Notice that you have also set the tag property, which is very useful for allowing you to search for particular
views during runtime.
You also create a Button view by calling the buttonWithType: method with the UIButtonTypeRoundedRect
constant. This method returns a UIRoundedRectButton object (which is a subclass of UIButton).
//---create a Button view---
frame = CGRectMake(10, 70, 300, 50);
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
[button setTitle:@”Click Me, Please!” forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
button.tag = 2000;

You then wire an event handler for its Touch Up Inside event so that when the button is tapped, the
buttonClicked: method is called:
[button addTarget:self
action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];

Finally, you add the label and button views to the view you created earlier:
[view addSubview:label];
[view addSubview:button];

Finally, you assign the view object to the view property of the current View Controller:
self.view = view;

Download Code



Now that you have installed the SDK, you are ready to start developing for the Iphone application. This tutorial enables you to user the various tools quickly without getting bogged down with the details. It also provides you with instant gratification: You see yourself that things really work, which can be morale booster that inspires you to learn more.

Getting Started with Xcode:

Power up Xcode and you should see the Welcome screen


To create a new iPhone project, choose File ➪ New Project. The different types of projects you can create using Xcode. The left panel shows the two primary categories — iPhone OS and Mac OS X. The iPhone uses the iPhone OS , so click the Application item listed under iPhone OS to view the different templates available for developing your iPhone application.

Name the project HelloWorld and click Save. Xcode proceeds to create the project for the template you have selected. Figure 2-3 shows the various fi les and folders automatically created for your project.



Interface Builder:


At this point, the project has no UI. To prove this, simply press Command-R (or select Run ➪ Run), and your application is deployed to the included iPhone 4 Simulator. The blank screen displayed on the iPhone Simulator. It’s good to see this now, because as you go through the chapter you will see changes occur based on your actions.


In the list of fi les in your project, you’ll notice two fi les with the .xib extension MainWindow.xib and HelloWorldViewController.xib. Files with .xib extensions are basically XML fi les containing the UI defi nitions of an application. You can edit .xib fi les by either modifying their XML content or, more easily (and more sanely), editing them using Interface Builder.

Double-click the HelloWorldViewController.xib fi le to launch Interface Builder..



After the Label view is added, select it and choose Tools ➪ Attributes Inspector. Enter Hello World! in the Text field . Then, next to Layout, click the center Alignment type.



With the Label view still selected, press Command-T to invoke the Fonts window. Set the font size to 36


Next, from the Library window, drag and drop a Text Field view to the View window, followed by a Round Rect Button view. Modify the attribute of the Round Rect Button view by entering Click Me! in the Title field.


Save the HelloWorldViewController.xib fi le by pressing Command-S. Then, return to Xcode and run the application again by pressing Command-R. The iPhone 4 Simulator now displays the modified UI.


Writing Some code:

By now you should be comfortable enough with Xcode and Interface Builder to write some code. This section will give you a taste of programming the iPhone.

In the HelloWorldViewController.h file, add a declaration for the btnClicked: action:
#import <UIKit/UIKit.h>
@interface HelloWorldViewController : UIViewController 
{
}
-(IBAction) btnClicked:(id) sender;
@end

The bold statement creates an action (commonly known as an event handler) named btnClicked:.

In the HelloWorldViewController.m file, add the code that provides the implementation for the btnClicked: action:

#import “HelloWorldViewController.h”
@implementation HelloWorldViewController
-(IBAction) btnClicked:(id) sender {
//--display an alert view--
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle:@”Hello World!”
message:@”iPhone, here I come!”
delegate:self
cancelButtonTitle:@”OK”
otherButtonTitles:nil];
[alert show];
[alert release];
}

The preceding code displays an alert containing the sentence “iPhone, here I come!”
That’s it! Go back to Xcode and run the application again. This time, when you click the Button view, an Alert view displays


Download Code


     
Welcome to the world of iPhone programming! This post   shows that you are fascinated with the idea of developing your own iPhone (and iPad) applications and want to join the ranks of the tens of thousands of developers whose applications are already deployed in the App Store. So Once again welcome to this iphone world. You will learn lots of interested things here. It will make interested things on you.....

1. Download & Install the Iphone SDK


To develop for the iPhone, you fi rst need to sign up as a registered iPhone developer at http://developer.apple.com/iphone/program/start/register/. Registration is free and provides you with access to the iPhone SDK (software development kit) and other resources that are useful for getting started.

At the time of writing, iOS (the iPhone OS) 4 is supported only on the iPhone and the iPod touch. The iPad still runs on the older iPhone OS 3.2 version. However, the iPhone SDK 4.0 supports both iPhone and iPad development. Because the iPad also uses the iPhone OS, throughout this book you will often see the term “iPhone” used.

After signing up, you can download the iPhone SDK.


Before you install the iPhone SDK, make sure you satisfy the following system requirements:

1.  Only Intel Macs are supported, so if you have another processor type (such as the older G4 or G5 Macs), you’re out of luck.

2.  Your system is updated with the latest Mac OS X release.

An actual iPhone/iPad is highly recommended, although not strictly necessary. To test your application, you can use the included iPhone Simulator (which enables you to simulate an iPhone or an iPad). However, to test certain hardware features like GPS, the accelerometer, and such, you need to use a real device.

When the SDK is downloaded, proceed with installing it. Accept a few licensing agreements and then select the destination folder in which to install the SDK.


If you select the default settings during the installation phase, the various tools will be installed in the /Developer/Applications folder.

2. Components Of The Iphone SDK:


The iPhone SDK includes a suite of development tools to help you create applications for your iPhone, iPod touch, and iPad. It includes the following:

1.  Xcode — Integrated development environment (IDE) that enables you to manage, edit, and debug your projects
2.  Dashcode — Integrated development environment (IDE) that enables you to develop webbased iPhone and iPad applications and Dashboard Widgets. Dashcode is beyond the scope of this book.
3.  iPhone Simulator — Provides a software simulator to simulate an iPhone or an iPad on your Mac.
4.  Interface Builder — Visual editor for designing user interfaces for your iPhone and iPad
applications
5.  Instruments — Analysis tool to help you both optimize your application and monitor for
memory leaks in real time

3. Xcode


To launch Xcode, double-click the Xcode icon located in the /Developer/Applications folder. Alternatively, go the quicker route and use Spotlight: Simply type Xcode into the search box
and Xcode should be in the Top Hit position.

Xcode Welcome screen.


Using Xcode, you can develop different types of iPhone, iPad, and Mac OS X applications using the various project templates.


Each template gives you the option to select the platform you are targeting — iPhone or iPad.

The IDE in Xcode provides many tools and features that make your development life much easier. One such feature is Code Sense, which displays a popup list showing the available classes and members, such as methods, properties, and so on.

4. Iphone Simulator:


The iPhone Simulator, is a very useful tool that you can use to test your application without using your actual iPhone/iPod touch/iPad. The iPhone Simulator is located in the /Developer/Platforms/iPhoneSimulator.platform/Developer/Applications folder. Most of the time, you don’t need to launch the iPhone Simulator directly — running (or debugging) your application in Xcode automatically brings up the iPhone Simulator. Xcode installs the application on the iPhone Simulator automatically.


The iPhone Simulator can simulate different versions of the iPhone OS.



In addition, the iPhone Simulator can simulate different devices — iPad, iPhone (3G and 3GS), and iPhone 4.



The iPhone Simulator simulating the older iPhone 3G/3GS and it simulating the iPad.


5. Uninstalling Applications from the iPhone Simulator


To uninstall (delete) an application, execute the following steps:



1 Click and hold the icon of the application on the Home screen until all the icons start wriggling. Observe that all the icons now have an X button displayed
on their top-left corner.
2 Click the X button (see Figure 1-13) next to the icon of the application you want to uninstall.
3 An alert window appears asking if you are sure you want to delete the icon. Click Delete to confirm the deletion.

6. Interface Builder


Interface Builder is a visual tool that enables you to design the user interfaces for your iPhone/iPad applications. Using Interface Builder, you drag and drop views onto windows and then connect the various views with outlets and actions so that they can programmatically interact with your code.

7. instruments


The Instruments application enables you to dynamically trace and profile the performance of your Mac OS X, iPhone, and iPad applications.



Using Instruments, you can do all of the following:

1.  Stress test your applications
2.  Monitor your applications for memory leaks
3.  Gain a deep understanding of the executing behavior of your applications
4.  Track diffi cult-to-reproduce problems in your applications