Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alert message for the button click in iphone sdk 4.3

I am beginner to xcode programming.Please tell me how to display the alert message when we are going to click the button in xcode-iphone-4.3

My Code is as follows,

- (IBAction)buttonPressed:(id)sender{

    UIAlertView* mes=[[UIAlertView alloc] initWithTitle:@"Hello World!!!!!!" 
                                                    message:@"This is the Iphone app" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];

    [mes show];

    [mes release];

Please help me regarding this.

like image 777
rani Avatar asked Jan 18 '12 06:01

rani


2 Answers

-(IBAction)buttonOnePressed:(id)sender
 {
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Clicked button 1"
                  message: @"Alert Message here"
                  delegate: self
                  cancelButtonTitle:@"Cancel"
                  otherButtonTitles:@"OK",nil];

     [alert setTag:1];
     [alert show];
 }

-(IBAction)buttonTwoPressed:(id)sender
 {
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Clicked button 2"
                  message: @"Alert Message here"
                  delegate: self
                  cancelButtonTitle:@"Cancel"
                  otherButtonTitles:@"OK",nil];

     [alert setTag:2];
     [alert show];
 }

Below is the delegate method to track which button on Alertview is hit.

  -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex   
   {
     if (alertView.tag == 1) { // UIAlertView with tag 1 detected
       if (buttonIndex == 0) 
       {
            NSLog(@"user pressed Button Indexed 0");
            // Any action can be performed here
       }
       else 
       {
            NSLog(@"user pressed Button Indexed 1");
            // Any action can be performed here
       }
     }

    else if (alertView.tag == 2) { // UIAlertView with tag 2 detected
       if (buttonIndex == 0) 
       {
            NSLog(@"user pressed Button Indexed 0");
            // Any action can be performed here
       }
       else 
       {
            NSLog(@"user pressed Button Indexed 1");
            // Any action can be performed here
       }
     }
   }

You can set tag to UIAlertView in case you have more than one UIAlertViews and can determine which UIAlertView button is clicked in its delegate method clickedButtonAtIndex using its respective tag.

like image 143
Suraj Mirajkar Avatar answered Nov 11 '22 13:11

Suraj Mirajkar


In IBAction you have to write the code and give the Connections to the Button

like image 30
Tendulkar Avatar answered Nov 11 '22 13:11

Tendulkar