簡體   English   中英

在iPhone中觸摸ActionSheet即可將其關閉

[英]Dismiss ActionSheet by touch outside of it in iPhone

我想在用戶點擊屏幕上其他任何位置時關閉UIActionsheet 我不想提供取消按鈕。

我的密碼

UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:sheetTitle delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];

    [sheet setTag:indexPath.row];

    for (NSString *buttonTitle in buttonTitles)
    {
        [sheet addButtonWithTitle:buttonTitle];
    }

    [sheet showFromToolbar:self.navigationController.toolbar];

    [sheet release];
    [buttonTitles release];

使您的ActionSheet對象成為全局對象,然后將其分配到您的塊中。 那么您可以將其從視圖上的外部觸摸事件中消除。 在您的視圖中添加以下塊。

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
  [sheet removeFromSuperView];
  sheet=nil;
}

希望這個能對您有所幫助

嘗試這個

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOut:)];
tap.cancelsTouchesInView = NO;
[actionSheet.window addGestureRecognizer:tap];
[tap release];`<br> `-(void)tapOut:(UIGestureRecognizer *)gestureRecognizer {
CGPoint p = [gestureRecognizer locationInView:self.actionSheet];

if (p.y < 0) {
  [actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
}

這是UIActionsheet默認功能,可以在touch outsidedismiss 無需其他代碼。

[sheet showFromToolbar:self.navigationController.toolbar];

在制造我認為的問題。 更改為

[sheet showInView:[UIApplication sharedApplication].keyWindow];

全局聲明ActionSheet,然后在viewdidload之后粘貼此代碼

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

        [super touchesBegan:touches withEvent:event];
        [actionSheet dismissWithClickedButtonIndex:0 animated:YES];
    }

工作100%

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM