Search code examples
iosuibuttonuiactionsheet

Change unbutton title from u inactionsheet button


I have a uibutton that displays an uiactionsheet. I want to replace the unbutton title with the uiactionsheet button title. The uiactionsheet button shows a radio band (AM/FM) choice. The uiactionsheet will select the band. I then want the uibutton title to show which mode was selected. What do I add the the "clickedButtonAtIndex" in the action sheet actions.

            - (IBAction)modeButton1:(id)sender
{

UIActionSheet *actionSheet2 = [[UIActionSheet alloc] initWithTitle:@"Mode Select"
                                                          delegate:self
                                                 cancelButtonTitle:@"Cancel"
                                            destructiveButtonTitle:nil
                                                 otherButtonTitles:@"FM",@"AM",nil];

 [actionSheet2 showFromRect:[(UIButton *)sender frame] inView:self.view animated:YES];
}

 -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
   {
    if (buttonIndex == 0)
   {
    NSLog(@"FM mode selected");
   }
   else if (buttonIndex == 1)
   {
     NSLog(@"AM mode selected");
    }
  }

Solution

  • Make a property for the button and in the clickedButton use setTitle:ForState: but firstly check that the button pressed is not the cancel button.