Search code examples
iosobjective-cuiactionsheet

How to Change UIActionSheet Button title on their Click?


i am showing UIActionSheet on tapping of UIbutton.

UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"Clock" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Start",@"Reset",nil];
[actionSheet setTag:1001];
[actionSheet showInView:self.view];

[actionSheet release];

I want to change the title of first "start" button on clicking.

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
 {
    switch (buttonIndex) 
    {
        case 0:
        {
          // I want to change title of first button start to stop. on clicking.
        }
           break;
        case 1:
        {

        }
          break;
        default:
            break;
    }

 }

enter image description here

Need change start button title to stop and when it is stop again to start .Vice Versa


Solution

  • You can change the text by using following code. Try to implement in .m File

    @interface AudioPlayerDemoViewController ()
    {
    UIActionSheet *action;
    NSString *titleString;
    }
    @end
    
    @implementation AudioPlayerDemoViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        titleString = @"Start";
        // Do any additional setup after loading the view, typically from a nib.
    }
    - (IBAction)actionSheetTap:(id)sender
    {
        action = [[UIActionSheet alloc]initWithTitle:@"Demo" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:titleString,@"Stop", nil];
        [action showFromBarButtonItem:sender animated:YES];
    
    }
    
    -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex];
    
        if([title isEqual: @"Start"])
        {
            action = nil;
            titleString = @"Pause";
    
        }
        if([title isEqual: @"Pause"])
        {
            action = nil;
            titleString = @"Start";
    
        }
    
    }