iosobjective-cuibuttonuialertviewuialertviewdelegate

UIButton title refresh after UIAlertView


I have a button with title Fun whitch is set from the storyboard

In viewDidLoad I change the title

self.catBut.titleLabel.text = @"Random";

And then when another button is click

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title alert" message:@"Alert message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];

But after the alert shows up the title of the button change back to Fun


Solution

  • .h file keep this

    @property(non atomic,strong)IBOutlet UIButton *catBut;
    

    use this for change the button title

    [self.catBut setTitle:@"Random" forState:UIControlStateNormal]; 
    

    use UIAlertViewDelegate Method

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title alert" message:@"Alert message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    alert.tag==100; //if u used multiple alert in your VC, use to identify the Tag
      [alert show];
    
    
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex    
    {
    if(alertView.tag==100)
    {
     [self.catBut setTitle:@"Random" forState:UIControlStateNormal]; 
     }
    else
      {
       // anotehr mthod optiobns
     }
     }