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