Problems with Cancel Button and UIActionSheet

前端 未结 3 1025
星月不相逢
星月不相逢 2021-02-19 20:13

How do I determine if the cancel button was pressed on a UIActionSheet?

My UIActionSheet is set up like this:

-(IBAction)fileButtonPressed
{
    UIAction         


        
相关标签:
3条回答
  • 2021-02-19 20:28

    add this

    [mymenu showInView:self.parentViewController.tabBarController.view];

    0 讨论(0)
  • 2021-02-19 20:35
    if (buttonIndex == actionSheet.cancelButtonIndex)
    {
        // Handle cancel action
    }
    

    UIActionSheet also has properties like destructiveButtonIndex and firstOtherButtonIndex to compare against.

    0 讨论(0)
  • 2021-02-19 20:40

    The trick turns out to be not to use the automatic cancel button but to add it yourself.

    The other slight gotcha is to add the cancel button at the end and not at the beginning.

    -(IBAction)fileButtonPressed
    {
        UIActionSheet *mymenu = [[UIActionSheet alloc] 
                                 initWithTitle:@"Select Folder" 
                                 delegate:self 
                                 cancelButtonTitle:nil 
                                 destructiveButtonTitle:nil 
                                 otherButtonTitles:nil];
        for (int nb=0; nb<3; nb++) 
        { 
            [mymenu addButtonWithTitle:@"Button Name"]; 
        }
    
        mymenu.cancelButtonIndex = [mymenu addButtonWithTitle: @"Cancel"];
    
        [mymenu showInView:self.view];
    }
    

    credit to this stackoverflow entry for the answer.

    0 讨论(0)
提交回复
热议问题