Zone

Ads

Saturday, July 14, 2012

This tutorial is for people who are new in iPhone programing environment and want to learn the basics of the iPhone programing.The alert view is used when you want to show any message to user and also it can be used as a debugging tool for checking any variable at runtime of application.
Now it is the time of action to develop an iPhone application showing alert view .we will describe step by step to develop
1.Using xcode create a new view based iPhone application project and name it AlertView.
2.There will be six files created in your left side namely
(i).AlertViewAppDelegate.h
(ii)AlertViewAppDelegate.m
(iii)MainWindow.xib
(iv)AlertViewViewController.h
(v)AlertViewViewControlle.m
(vi)AlertViewViewControlle.xib

In these six files open the AlertViewViewController.h file and add alertview delegatd

#import UIKit.h>

@interface AlertViewViewController : UIViewController {

}

@end

Now open the AlertViewViewController.m file and it will look like so as described below

#import "alertViewsViewController.h"

@implementation alertViewsViewController

- (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.
}

#pragma mark - View lifecycle

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{

[super viewDidLoad];
}

- (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

AlertView is a view which is not present in object liberary so we have to create it programatically how we can do this is described below

UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"Hello world" message:@"This is an alert view" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
In this way we create an object of class UIAlertView and set the title "Hello world" and the message "This is an alert view " and the title of button as ok.
Here we have also to show alert view and for this we have use UIAlertView class method "show" to show alertview on the screen.
And that method is implemented as below in the code

[alert show];
so we will implement these two lines in the viewDidLoad method .And our code will be look like so

#import "alertViewsViewController.h"

@implementation alertViewsViewController

- (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.
}

#pragma mark - View lifecycle

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"Hello world" message:@"This is an alert view" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
[alert show];
[super viewDidLoad];
}

- (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

Now save your code and run.Now the application will launch and an alert view will be shown on the screen

No comments:

Post a Comment