无法解雇2个UIActionSheets

时间:2011-10-20 22:15:50

标签: iphone objective-c cocoa-touch uiactionsheet

我在UIToolbar中有2个UIBarButoon。每个人都有一个IBAction来发布他们自己的UIActionSheets。

现在,如果我提出一个,然后点击另一个UIBarButton,另一个ActionSheet也会出现,所以我在屏幕上有两个重叠。我希望一个人在提出另一个时解雇,等等。

我现在有了这个代码:

- (IBAction)showFirstActionSheet:(id)sender
{
    if (!self.eRXActionSheet)
    {
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"eRX Menu" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"New eRX", @"eRX Refills", nil];
        sheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
        self.eRXActionSheet = sheet;
        [sheet release];

        [self.eRXActionSheet showFromBarButtonItem:sender animated:YES];

        return;
    }

    [self.eRXActionSheet dismissWithClickedButtonIndex:self.eRXActionSheet.cancelButtonIndex animated:YES];
    self.eRXActionSheet = nil;
}

- (IBAction)showSecondActionSheet:(id)sender
{
    if (!self.actionSheet)
    {
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Action Menu" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Help", @"Lock", @"Log Out", nil];
        sheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
        self.actionSheet = sheet;
        [sheet release];

        [self.actionSheet showFromBarButtonItem:sender animated:YES];

        return;
    }

    [self.actionSheet dismissWithClickedButtonIndex:self.actionSheet.cancelButtonIndex animated:YES];
    self.actionSheet = nil;
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet == self.eRXActionSheet)
    {
        if (buttonIndex == 0)
        {
            [self erxButtonClicked];
        }
        else if (buttonIndex == 1)
        {
            [self erxRefillButtonClicked];
        }
    }
    else
    {
        if (buttonIndex == 0)
        {
            [self helpButtonClicked];
        }
        else if (buttonIndex == 1)
        {
            [self lockButtonClicked];
        }
        else if (buttonIndex == 2)
        {
            [self logOut];
        }
    }
}

2 个答案:

答案 0 :(得分:1)

在您的代码中,在创建操作表之前,您正在检查要显示的操作表是否为零。从逻辑上讲,这将始终评估为true。

相反,您应该检查其他操作表是否为零。如果是这样,请将其解雇并创建新的。

-(void)buttonForActionSheet1Clicked {
    if (actionSheet2 != nil) {
        // dismiss it
    }
    // show actionSheet1
}

-(void)buttonForActionSheet2Clicked {
    if (actionSheet1 != nil) {
        // dismiss it
    }
    // show actionSheet2
}

或者,您可以通过禁用工具栏中的其他按钮强制用户首先使用取消按钮关闭当前操作表。

答案 1 :(得分:1)

在您要解雇的操作表上调用dismissWithClickedButtonIndex:animated:

E.g。在showFirstActionSheet中添加此行以关闭另一行:

if(self.actionSheet) [self.actionSheet dismissWithClickedButtonIndex:<indexofCancelButton> animated:YES];