UIAction Sheet не исчезает при нажатии UIBarButton - PullRequest
2 голосов
/ 14 октября 2011

Я представляю UIActionSheet от UIBarButtonItem.Я хочу, чтобы лист действий отклонялся, когда я снова нажимал кнопку панели, вместо этого он создавал новый каждый раз, когда накладывал один поверх другого.Есть идеи?

- (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];
    }
}

1 Ответ

6 голосов
/ 14 октября 2011

Я бы объявил @property для popupQuery в своем классе и использовал его для отслеживания листа действий.

- (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;
}
...