在iphone sdk 4.3中单击按钮的警报消息

ran*_*ani 7 iphone xcode alert onclick button

我是xcode编程的初学者.当我们要点击xcode-iphone-4.3中的按钮时,请告诉我如何显示警告信息

我的守则如下,

- (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];
Run Code Online (Sandbox Code Playgroud)

请帮我解决这个问题.

Sur*_*kar 22

-(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];
 }
Run Code Online (Sandbox Code Playgroud)

下面是跟踪Alertview上的哪个按钮的委托方法.

  -(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
       }
     }
   }
Run Code Online (Sandbox Code Playgroud)

您可以设置标记UIAlertView,以防您有多个UIAlertViews,并可以使用其相应的标记确定UIAlertView在其委托方法中单击了哪个按钮clickedButtonAtIndex.