Saturday, April 14, 2012

Display an AlertView on Button Touch

In this post i will be explaining how to display your name in an alert view.


Alert view: An alert view is like a message box in which you give notifications to the user regarding any particular error or any sort of message that you want.
So here's my basic view 




Now all i want to display here is the text of the Textfield in my alert view, so i will go step wise here.

Step 1: Open Xcode and create a window based application and give it an appropriate name, i have given it AlertDemo
Step 2: Create a class of UIViewController type and create objects of label, button and a text field also we will require a function where we will write the code to display an Alert view, heres what the Myviewcontroller.h file looks like this

@interface Myviewcontroller : UIViewController {
UILabel *lbl;
UIButton *btn;
UITextField *txtfield;
}
-(void)buttonTouch; //function which will display alert view on button touch
@end

now coming to the Myviewcontroller.m file where we will initialize these objects and give logic to our view, the init method will look like this

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
//setting the label
lbl = [[UILabel alloc]initWithFrame:CGRectMake(331014921)];
lbl.text = @"Name";
//setting the button
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(1191407237)];//setting the frame
[btn setTitle:@"Touch" forState:UIControlStateNormal]; //setting the text and state of button

[btn addTarget:self action:@selector(buttonTouchforControlEvents:UIControlEventTouchUpInside]; //calling the function
//setting the text
txtfield = [[UITextField alloc]initWithFrame:CGRectMake(9010113031)];
[txtfield setBorderStyle:UITextBorderStyleRoundedRect];
    }
    return self;
}

setting the buttonTouch method

-(void)buttonTouch
{
UIAlertView *objalert = [[UIAlertView alloc]initWithTitle:@"Alert box demo" message:txtfield.text delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:@"Cancel",nil];
[objalert show];
}

In the above code what i have done is created the object of UIAlertview, so now lets have a look at its parameters

1. initWithTitle: String that appears as the title of the message.
2.message: The initial message that you want to show to the user.
3. delegate: If you have specified some delegate to the alertview then set self else set it to nil.
4. cancelButtonTitle: The title of the cancel button else you can write nil if no cancel button.
5. otherButtonTitles: The title of another button.

Finally you write a nil in order to specify that their are no more button to be added to the alert view


now coming to the function called loadView,



- (void)loadView {
[super loadView];
[self.view addSubview:btn];
[self.view addSubview:txtfield];
[self.view addSubview:lbl];
}

Now we will go into the app delegate.m file and add the view to our window

#import "AlertDemoAppDelegate.h"
#import "Myviewcontroller.h"

@implementation AlertDemoAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    // Override point for customization after application launch
Myviewcontroller * obj = [[Myviewcontroller alloc]init];
[window addSubview:obj.view];
 [window makeKeyAndVisible];
}


now after that you can touch the button and after that you can see the following result on the iPhone simulator.


So this was my post on how to display a simple alert view on the button touch, i hope my this post is useful to you.
Happy iCoding.

No comments:

Post a Comment