Pop Up buttons in Interface Builder don't show menu in iOS 15

Hi, guys. I'm testing the new pop-up and pull-down buttons in iOS 15, but they do not show the pop-up menu when pressed (all the options in the Attributes Inspector panel are checked). They only work when I define the entire menu in code. My questions are:

  • Is this because the feature was not implemented yet?
  • If this is implemented later and we can define the menu in Interface Builder, how do you respond to a selection? In code, I can specify the action in the closure, but how do I do it if the menu is defined in Interface Builder?

This is how I define the menu from code, just in case someone needs to know:

import UIKit

class ViewController: UIViewController {
  @IBOutlet weak var myButton: UIButton!

  override func viewDidLoad() {
   super.viewDidLoad()
    
   let optionsClosure = { (action: UIAction) in
     print(action.title)
   }
   myButton.menu = UIMenu(children: [
     UIAction(title: "Option 1", state: .on, handler: optionsClosure),
     UIAction(title: "Option 2", handler: optionsClosure),
     UIAction(title: "Option 3", handler: optionsClosure)
   ])
  }
}

Thanks JD

Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

Your code gives this error and doesnt work

You need to set the changesSelectionAsPrimaryAction & showsMenuAsPrimaryAction to true for menu's to show.

This API is so weird. I don't know why they didn't just copy the API from NSPopUpButton.

You can wire a selector to the menu items in Interface Builder (apparently no target though...) so the items must dig through the responder chain to find something that implements the selector.

If you don't wire the selector for each menu item in IB, then the menu won't show up when you tap the button.

So bizarre.

Not sure if I'm doing something wrong but I also have to manually change the title on the button in the action

-(IBAction)popUpButtonAction:(UICommand*)sender
{
    self.mainPopUpbutton.title = sender.title;
}

Doing the above fixes the button title problem... but next time the button is clicked, the wrong menu item has the checkmark. I tried changing the state in the action method, but that throws an exception:


-(IBAction)popUpButtonAction:(UICommand*)sender
{
    self.mainPopUpbutton.title = sender.title;
    sender.state = UIMenuElementStateOn; // not allowed..
}

Also be careful wiring an UIButton IBOutlet to the pop up button (have to do it in the document outline... if you try to wire it to the button in a storyboard scene it actually wires the UIButton IBOutlet to the UIMenu!)

Implementing one of these in a UITableview is awful BTW.


Pop Up buttons in Interface Builder don't show menu in iOS 15
 
 
Q