如何在推送UINavigationController之后向UINavigationBar添加編輯按鈕

[英]How to add edit button to UINavigationBar after push on UINavigationController


I want to add the edit button of a table view to the navigation bar of a navigation controller after pushing this new table view on the navigation controller. When I do that, the edit button doesn't appear (I suspect the 'Back' button obscures it), and when I go back up the hierarchy, there it is, but now I have two of them (Edit Edit). My source code:

我想在導航控制器上推送這個新的表格視圖后,將表格視圖的編輯按鈕添加到導航控制器的導航欄。當我這樣做時,編輯按鈕不會出現(我懷疑“后退”按鈕會遮擋它),當我回到層次結構時,它就是,但現在我有兩個(編輯編輯)。我的源代碼:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  let items:UITableViewController = ItemsController()
  myNavigationController.pushViewController(items, animated:true)
  myNavigationController.navigationBar.topItem!.leftBarButtonItems! += [ items.editButtonItem() ]
}

EDIT

If I comment out the last line above, and insert the following line before push, the Edit button appears on the left, but there is now no Back button:

如果我注釋掉上面的最后一行,並在推送之前插入以下行,則左側會顯示“編輯”按鈕,但現在沒有“返回”按鈕:

items.navigationItem.rightBarButtonItem = items.editButtonItem()

SOLUTION

The solution I am using in viewDidLoad() of ItemsController():

我在ItemsController()的viewDidLoad()中使用的解決方案:

    var items = navigationItem.rightBarButtonItems
    if items == nil {
        items = [UIBarButtonItem]()
    }
    items! += [ editButtonItem() ]
    navigationItem.rightBarButtonItems = items

1 个解决方案

#1


1  

Both the left and right bar buttons of a UINavigationItem can be represented as a single barButtonItem or an array of barButtonItems. You need to access and set the latter (which adds a button instead of just replacing the Back button). To do this within the same viewController, use this code:

UINavigationItem的左右欄按鈕都可以表示為單個barButtonItem或barButtonItems數組。您需要訪問並設置后者(添加按鈕而不是僅替換后退按鈕)。要在同一個viewController中執行此操作,請使用以下代碼:

if var leftBarButtonItems = self.navigationItem.leftBarButtonItems{
            leftBarButtonItems.append(self.editButtonItem())
            self.navigationItem.leftBarButtonItems = leftBarButtonItems
        }

However, if at all possible I would use the above code to put the Edit button on the right side, as that is where it is expected.

但是,如果可能的話,我會使用上面的代碼將Edit按鈕放在右側,因為這是預期的位置。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/03/12/720c6c544c2877ef07e2569e209a93ba.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com