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;




































