首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按UIBarButton键时未清除UIAction工作表

按UIBarButton键时未清除UIAction工作表
EN

Stack Overflow用户
提问于 2011-10-14 07:22:03
回答 1查看 1.9K关注 0票数 2

我正在演示一个来自UIBarButtonItem的UIActionSheet。当我再次单击工具栏按钮时,我希望动作单消失,而不是每次都在彼此的顶部创建一个新的动作单。有什么想法吗?

代码语言:javascript
复制
- (IBAction)actionButtonClicked:(id)sender
{
    UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@"Action Menu" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Help", @"Lock", nil];
    popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [popupQuery showFromBarButtonItem:sender animated:YES];
    [popupQuery release];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        [self erxButtonClicked];
    }
    else if (buttonIndex == 1)
    {
        [self erxRefillButtonClicked];
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-10-14 07:30:11

我会在我的类中为popupQuery声明一个@property,并使用它来跟踪动作单。

代码语言:javascript
复制
- (IBAction)actionButtonClicked:(id)sender
{
    // If self.popupQuery is not nil, it means the sheet is on screen and should be dismissed. The property is then set to nil so a new sheet can be created next time.
    if (self.popupQuery)
    {
        [self.popupQuery dismissWithClickedButtonIndex:self.popupQuery.cancelButtonIndex animated:YES];
        self.popupQuery = nil;

        return;
    }

    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Action Menu" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Help", @"Lock", nil];
    sheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    sheet.delegate = self;
    self.popupQuery = sheet;
    [sheet release];

    [self.popupQuery showFromBarButtonItem:sender animated:YES];  
}

// Implementing this method to be notified of when an action sheet dismisses by means other than tapping the UIBarButtonItem. We set the property to nil to prepare for lazy instantiation next time.
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    self.popupQuery = nil;
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7761546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档