簡體   English   中英

加載視圖后,是否可以在工具欄上添加,刪除或隱藏按鈕?

[英]Is it possible to add, delete or hide buttons on a toolbar after the view has loaded?

我試圖根據表格視圖的狀態來更改工具欄上的可見按鈕。 這可能嗎?

要添加更多詳細信息:我目前在表格視圖中有一個練習列表。 底部有一個工具欄,上面有一個“新”按鈕,允許用戶創建新的練習。 當用戶單擊右上角的“編輯”時,視圖將進入編輯模式,我希望他們能夠通過單擊工具欄中的現在未隱藏的“刪除”按鈕來使用多項選擇刪除練習。 在這一點上,我試圖使“新”按鈕消失,僅保留“刪除”按鈕。

我可以找到許多有關如何添加工具欄和向其添加按鈕的答案,但是一旦視圖加載,似乎沒有一個可以解決這個問題。

我考慮過要使用兩個不同的工具欄,每個工具欄上都有單獨的按鈕,然后切換隱藏的和可見的,但這會導致其他問題,所以我在研究是否有一種簡單的方法可以僅隱藏添加的按鈕。

真的很簡單。 首先讓我們看一下如何創建UIToolbar,然后向其中添加項目,然后在運行時進行更改

self.MNToolbar=[[UIToolbar alloc] init];

self.addButton=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(importPhotos:)];

self.flexibleSpace=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

self.MNToolbar.items=[[NSArray alloc] initWithObjects:flexibleSpace, addButton, flexibleSpace, nil];

[self.view addSubView:self.MNToolbar];
// don't forget to set the bounds too

這是創建它的方法。

好的,現在讓我們看看如何在運行時進行更改

self.deleteButton=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(deletePhotos:)];

self.MNToolbar.items=[[NSArray alloc] initWithObjects:self.deleteButton, nil];

我希望你明白這一點。 如果您有任何問題,請通知我。

暫無
暫無

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

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