How to get the title of the selected UIActionSheet button into a label?
Asked Answered
D

1

6

I have a button with some options.

- (IBAction)monstrarActionSheet:(id)sender {
    UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"Titulo"
                                                        delegate:self
                                               cancelButtonTitle:@"Mejor no" destructiveButtonTitle:@"Que miedo" otherButtonTitles:@"Otro boton 1", @"Otro boton 2", nil];
    [action showInView: self.view];

When an option is selected I show a message in logs to show which button was selected.

-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    /*if(actionSheet.tag == 1){

    }*/

    NSLog(@"El usuario seleciono el boton %ld", (long)buttonIndex);
}

And in my ViewController.h my button and label are defined like that

@interface ViewController : UIViewController <UIActionSheetDelegate>

@property (weak, nonatomic) IBOutlet UILabel *lbViewController1;

How can I put the selected UIActionSheet's button's title into my lbViewController1 label?

Diaphysis answered 18/2, 2015 at 1:53 Comment(2)
Put what value in your label?Binoculars
The text value of the option selected @LyndseyScottDiaphysis
B
15

UIActionSheet has been deprecated so you should be using UIAlertController of type action sheet for iOS 8.0+, but to answer your question using your current code, you can set your label to contain the button's title like so:

-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    /*if(actionSheet.tag == 1){

    }*/
    NSLog(@"El usuario seleciono el boton %ld", (long)buttonIndex);
    self.lbViewController1.text =  [actionSheet buttonTitleAtIndex:buttonIndex];
}
Binoculars answered 18/2, 2015 at 2:0 Comment(1)
Your question's wording is a bit confusing though, so let me know if I didn't answer your question...Binoculars

© 2022 - 2024 — McMap. All rights reserved.