Detecting button click with UIAlertView

后端 未结 6 1273
难免孤独
难免孤独 2021-01-01 12:10

I am trying to call and alert when a button is pressed. i use this :

-(IBAction)Add { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @\"add bu         


        
6条回答
  •  太阳男子
    2021-01-01 12:31

    This is your code which i used and added some my code also. **

    -(IBAction) Add 
    {
    
              UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"add button pressed"
    
                                                           message:@"Add to record"
                                                          delegate:nil     
                                                 cancelButtonTitle:@"Cancel" 
                                                 otherButtonTitles:@"OK", nil];
    
           alert.tag=101;//add tag to alert
           [alert show];
           [alert release];     
    }
    

    Now when you press button on alert it will call clickedButtonAtIndex but there should a identifier for every alert. So add tag and then

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex** 
    
    {
    
    //  the user clicked one of the OK/Cancel buttons
    if(alertView.tag == 101)     // check alert by tag 
    {    
    
    if (buttonIndex == 0)
    
       {
    
        //just to show its working, i call another alert view
    
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"OK WORKIng well"
    
                                                        message:@"no error"
                                                       delegate:nil
                                              cancelButtonTitle:@"IWORKS" 
                                              otherButtonTitles:@"NO PRB", nil];
    
        [alert show];
        [alert release];    
    
    }
    
    else
    
    {
        NSLog(@"cancel");       
    }
    }
    }
    

    Hope it helps.

提交回复
热议问题