簡體   English   中英

在iPad上從UIBarButtonItem中刪除UIActionSheet

[英]Dismissing UIActionSheet from UIBarButtonItem on an iPad

我有一個帶有幾個UIBarButtonItemUIToolbar ,它使用showFromBarButtonItem:顯示各種UIActionSheet showFromBarButtonItem: .

在iPad上,當其中一個動作表出現在屏幕上時,觸摸動作表外的任何位置都會將其刪除而不執行任何操作(例如,觸摸按鈕不會觸發按鈕)。 這是設計 - 我不滿意的東西,但我接受它,只要它是通常的行為。

但有一個例外。 如果我觸摸另一個UIBarButtonItemUIBarButtonItem觸發此按鈕,並且不會從屏幕上刪除當前操作表。 如果新按鈕恰好啟動另一個UIActionSheet ,我最終會在屏幕上顯示兩個(或更多)操作表。

當然,我可以通過一個繁瑣的過程來記住屏幕上的內容並手動解除它,但我也關注用戶,因為一些觸摸(那些以工具欄按鈕為目標)被尊重而其他人被忽略。

有什么我可以做或者我必須忍受這種情況嗎?

這似乎是一個惱人的不一致,但不是一個難以糾正。 這個例子假設你只需要顯示UIActionSheets ,就像你的情況一樣。

這是一個如何解決這個問題的功能示例,從.h開始:

@interface TestToolBarExclusiveActionSheet : UIViewController <UIActionSheetDelegate>{
    UIActionSheet *sheetShowing;
}
@property (weak, nonatomic) IBOutlet UIBarButtonItem *oneButton;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *twoButton;
@end

.m

#import "TestToolBarExclusiveActionSheet.h"
@implementation TestToolBarExclusiveActionSheet
@synthesize oneButton;
@synthesize twoButton;
-(IBAction)targetForBothButtons:(UIBarButtonItem *)button{
    if (sheetShowing != nil){
        [sheetShowing dismissWithClickedButtonIndex:[sheetShowing cancelButtonIndex] animated:YES];
        sheetShowing = nil;
    }
    else{ // I am free to act on the button push.
        // Just for demo.. So:
        sheetShowing = [[UIActionSheet alloc] initWithTitle:button.title delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Destroy" otherButtonTitles:nil, nil];
        [sheetShowing showFromBarButtonItem:button animated:YES];
    }
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    sheetShowing = nil;
    // Respond to action sheet like normal
}
-(void)viewDidUnload{
    [self setOneButton:nil];
    [self setTwoButton:nil];
    [super viewDidUnload];
}
@end

最后是.xib的截圖(在iPhone模式下顯示圖像大小):

在此輸入圖像描述

只需連接按鈕出口並將兩個按鈕操作連接到targetForBothButtons:方法。

您將看到,如果其中一個操作表顯示,則按任意欄按鈕將導致解除操作表。

暫無
暫無

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

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