在哪里向 UIMenuController 添加自定义菜单项?
我创建了 UITextView 的子类,现在我想通过添加一些按钮来扩展默认的 UIMenuController 。执行扩展完全没有问题,我只需要添加一些 UIMenuItem
,以便添加 UIMenuController
共享实例的 menuItems
数组。
我现在的问题是:我在哪里执行此操作? MenuController 是一个单例实现,因此我所做的每个更改都会影响支持该菜单的所有其他视图。那么我要在哪里添加额外的菜单项以及稍后在哪里删除它们呢?我正在寻找某种与 UIViewController 的 viewWillAppear 方法等效的方法。
或者我是否担心太多了,在包含我的 UITextView 子类的视图的视图控制器中实现它是一个很好的做法? 但是,这意味着包含我的类作为子视图的每个视图控制器都必须实现相同的代码。有更好的办法吗?
编辑:另一种选择当然是保持我的类的选择器唯一,并让所有其他自定义视图为 -respondsToSelector:
中的选择器返回 NO
。这对我来说似乎是迄今为止最好的解决方案。
最佳实践是什么?
I created a subclass of UITextView and now I want to extend the default UIMenuController
by adding some buttons. Performing the extension is no problem at all, I just need to add some UIMenuItem
so the menuItems
array of the shared instance of the UIMenuController
.
My question is now: Where do I perform this manipulation? The MenuController is a singleton implementation, so every change I make will affect all the other views that support the menu. So where am I going to add the extra menu items and where do I later remove them again? I am searching for some kind of equivalent to the UIViewController's viewWillAppear
method.
Or am I worrying way too much and it is perfectly good practice to implement it in the view controller of the view containing my UITextView
subclass?
However, this means that every view controller containing my class as subview would have to implement the same code. Is there a better way?
Edit: Another option is of course to keep the selectors for my class unique and have all other custom views return NO
for the selector in -respondsToSelector:
. This seems to be the best solution so far to me.
What is the best practice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明,在应用程序委托中添加项目效果很好。当然,您需要确保支持 MenuController 的其他视图对于
-respondsToSelector:
中的特定选择器返回NO
。It turns out that it works fine to add the items in the app delegate. You need to make sure of course, that other Views supporting the MenuController return
NO
for your particular selector in-respondsToSelector:
.好问题。不确定最佳实践。
您可能想使用 viewDidAppear 和 viewWillDisappear 来修改它。
Good question. Not sure about best practice.
You probably want to use the viewDidAppear and viewWillDisappear to modify that.