Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8542873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:10:12+00:00 2026-06-11T12:10:12+00:00

I am trying to detect whether the user pressed the cancelButton or destructive button

  • 0

I am trying to detect whether the user pressed the cancelButton or destructive button on the actionSheet. Depending on his/her input, I would like to modify the alert message shown on the screen.

So I tried using if to check the buttonIndex, but it turns out that the buttonIndex value does not change with the input from the user.

UIActionSheet *informUser = [[UIActionSheet alloc] initWithTitle:@"Missing data" delegate:self cancelButtonTitle:@"Thanks for telling me!" destructiveButtonTitle:@"I have ignored it!" otherButtonTitles:nil];

    [informUser showInView:self.view];

//        NSLog(@"Cancel button = %d", informUser.cancelButtonIndex);
//        NSLog(@"Destructive button = %d", informUser.destructiveButtonIndex);

    if(informUser.cancelButtonIndex == 1)
    {
        NSString *msg = [[NSString alloc] initWithFormat:@"Pls enter the number."];

        UIAlertView *alertUser = [[UIAlertView alloc] initWithTitle:@"Missing data" message:msg delegate:self cancelButtonTitle:@"Thanks!" otherButtonTitles:nil];

        [alertUser show];
    }

I also tried using a separate method:

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex;

but, I get wrong results when I have multiple UIAlert at different points on the same screen.

In short, I would like to use a different UIAlert for different actions/inputs from user. Therefore, how do I detect which button on the actioSheet has been pressed by the user?

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:10:14+00:00Added an answer on June 11, 2026 at 12:10 pm

    Implement the UIActionSheetDelegate and use the method:

    – actionSheet:clickedButtonAtIndex: 
    

    The index defines which button you tapped.

    If you don’t want to use the button index you can get the button title of the tapped button with

    – buttonTitleAtIndex:
    

    Then you can compare the result to your button title string.

    Read more in the Apple docs


    Edit:
    If you want to use more actionsheets you can set the tag-value for each of them. E.g. ActionSheet1 has actionsheet.tag = 1 and so on.

    Then you can switch in – actionSheet:clickedButtonAtIndex: by the tag of the given actionsheet. E.g.:

    switch (actionsheet.tag) {
        case 1: 
            // do stuff
        break;
        case 2: 
            // do stuff
        break;
        default: break;
    }
    

    Edit 2:
    This is a sample class that should show you the logic:

    Header file:

    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController <UIActionSheetDelegate>
    
    @end
    

    Implementation file:

    #import "ViewController.h"
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        UIActionSheet *as1 = [[UIActionSheet alloc] initWithTitle:@"Title" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles:@"Ok", nil];
        as1.tag = 0;
        as1.delegate = self;
    
        UIActionSheet *as2 = [[UIActionSheet alloc] initWithTitle:@"Title2" delegate:self cancelButtonTitle:@"Cancel2" destructiveButtonTitle:@"Delete2" otherButtonTitles:@"Ok2", nil];
        as2.tag = 1;
        as2.delegate = self;
    }
    
    
    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
        switch (actionSheet.tag) {
            case 0: // as1
                switch (buttonIndex) {
                    case 0:
                        // button index 0
                        break;
    
                    case 1:
                        // button index 1
                        break;
    
                    //....
    
                    default:
                        break;
                }
                break;
    
            case 1: // as2
                switch (buttonIndex) {
                    case 0:
                        // button index 0
                        break;
    
                    case 1:
                        // button index 1
                        break;
    
                        //....
    
                    default:
                        break;
                }
                break;
    
            default:
                break;
        }
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to detect whether a button is pressed using AJAX and PHP (ofcourse
I'm trying to detect whether a user quickly presses a keyboard key twice, a
I'm trying to take a raw input and detect whether it is in a
I am trying to work out how to detect whether a user is running
I'm trying to detect whether the shift key is being pressed while the cursor
I'm trying to detect whether or not the user have save their work before
I am trying to detect whether a block of text (from a textarea) contains
I'm trying to detect whether an image exists on a remote server. However, I've
In a BASH script, I'm trying to detect whether a file exists. The filename
I am trying to create a script to detect whether a directory exists, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.