简体   繁体   中英

Where to add custom menu items to UIMenuController?

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?

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: .

Good question. Not sure about best practice.

You probably want to use the viewDidAppear and viewWillDisappear to modify that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM