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

时间:2012-01-18 06:34:25

标签: iphone xcode button onclick alert

我是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];

请帮我解决这个问题。

3 个答案:

答案 0 :(得分: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];
 }

以下是跟踪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
       }
     }
   }

如果您有多个UIAlertView,可以将代码设置为UIAlertView,并且可以使用其各自的代理方法UIAlertView确定单击了哪个clickedButtonAtIndex按钮标签

答案 1 :(得分:1)

在IBAction中,您必须编写代码并将连接提供给Button

答案 2 :(得分:0)

为您的按钮创建IBAction,并在该方法中添加警报视图的代码。