簡體   English   中英

iOS上帶有Storyboard的TableView導航欄中的“返回”,“編輯”和“添加”按鈕

[英]Back, Edit and Add buttons in navigation bar of TableView with Storyboard on iOS

我在使用導航欄上的“返回”,“編輯”和“添加”按鈕來實現表視圖時遇到一些問題。 通過單擊另一張表格視圖的一行可以到達該表格視圖,因此將自動添加“返回”按鈕。 使用情節提要,我已將“添加”按鈕添加到導航欄中。 在代碼中,我添加了“編輯”按鈕( 我使用了代碼,因為如果在情節提要中添加按鈕,我不知道如何重現“編輯”標准行為... ):

self.navigationItem.leftBarButtonItem = self.editButtonItem;

問題在於,以這種方式,“編輯”按鈕隱藏了導航欄上的“上一步”按鈕。

在這一點上,我有兩個問題:

  1. 情節提要板是否可以在導航欄上添加第三個按鈕?
  2. 萬一我必須以編程方式執行此操作,我知道可以按照以下步驟進行操作:

     UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect]; [button setFrame:CGRectMake(width-90,6,50,30)]; [button setTitle:@"Edit" forState:UIControlStateNormal]; button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; [self.navigationController.navigationBar addSubview:button]; 

但是,如何通過代碼實現“編輯”按鈕的標准行為? 我的意思是,我單擊“編輯”,按鈕變為“完成”,並且行變為可刪除...

在此先感謝,yassa

萬一其他人碰巧也碰到了這個問題,解決方案就很容易了。 UINavigationItem具有rightItems的屬性,而該屬性只是UIBarButtonItems的數組。 將添加按鈕和編輯按鈕都放入數組中,並將其分配給rightItems並完成操作:-)這是示例代碼片段:

UITableViewController *table = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self     action:@selector(insertNewObject:)];
NSArray *barButtons = [NSArray arrayWithObjects:table.editButtonItem, addButton, nil];
table.navigationItem.rightBarButtonItems = barButtons;

首先,Apple文檔說“您不直接將子視圖添加到導航欄”。 不知道這是否足以使應用程序從商店退回,但不認為它是“正確的”。

其次,您可以在iOS 5中向UINavigationItem添加三個以上的按鈕,但在iOS 4或更早版本中不能。

最后,我將把編輯按鈕保留在右上方,然后將其保留在左上方。 那就是人們期望他們的地方。 如果我想要添加按鈕(在iOS 5上),可以將其放在“編輯”按鈕旁邊。

抱歉; 沒有情節提要上的幫助。 對他們一無所知。

暫無
暫無

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

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