I am basically trying to replicate the "Replace ..." edit menu behavior (but with my own function different than Replace).
I have read a lot of posts about others having problems with UIMenuController and don't have any of the known problems.
In a UITextView, I select some text. The edit menu comes up with the expected items, including my "Translate..." option. When I click on "Translate..." in the menu, the menu closes and invokes my selector. That code changes the menu items to the sub-choices I want. I call
Any suggestions greatly appreciated!
Barry
I have read a lot of posts about others having problems with UIMenuController and don't have any of the known problems.
In a UITextView, I select some text. The edit menu comes up with the expected items, including my "Translate..." option. When I click on "Translate..." in the menu, the menu closes and invokes my selector. That code changes the menu items to the sub-choices I want. I call
but the menu is not re-shown. ("self" is a sub-class of UIViewController.) In testing this, I see that canPerformAction() is called as expected. I do get a call to my notification for willShowWindowNotification before the menu is shown the first time, but I do NOT get that notification before the menu should be re-displayed. (I do get the expected calls to canPerformAction(), verifying that the menu items are valid, but nothing after that.) If I move one of the end-points of the selected text, this causes the menu to be shown again, and it has my sub-menu items in it as expected. I know this should work because "Replace..." does it all the time. My code is shown below.UIMenuController.shared.showMenu(from: self.view, rect: windowBounds)
Any suggestions greatly appreciated!
Barry
Code Block swift @objc private func translateSelectionMenu() { let sharedMC = UIMenuController.shared // Create menu choices for the translate sub-menu. let charChoice = UIMenuItem(title: "To Chars", action: #selector(translateChars)) let byteChoice = UIMenuItem(title: "Byte Decimal", action: #selector(translateByte)) let halfChoice = UIMenuItem(title: "2-Byte Decimal", action: #selector(translateHalf)) savedMenuItems = sharedMC.menuItems sharedMC.menuItems = [charChoice, byteChoice, halfChoice] // Figure out where to put the menu. // Given my view layout, the menu is always going to be above the selected text. // Look at all the real selection rects to find the highest and widest rectangle. ... code omitted for brevity. This determines the selected rectangle coordinates. Values plugged in below. let textBounds = CGRect(x: 114.1, y: 73, width: 48, height: 55) let windowBounds = TextView.convert(textBounds, to: nil) // Results: 149.1, 284, 48, 55 // sharedMC.update() TextView.becomeFirstResponder() // Unneeded since it already is first responder. sharedMC.showMenu(from: self.view, rect: windowBounds) // No errors, just does nothing. }