【问题标题】:Swift: Why does "Pop Up Button" throw NSInternalInconsistencyException?Swift:为什么“弹出按钮”抛出 NSInternalInconsistencyException?
【发布时间】:2022-12-16 06:47:40
【问题描述】:

我在此处的 WWDC 视频之后创建了一个“弹出按钮”:

"Build interfaces with style" https://developer.apple.com/videos/play/wwdc2021/10196/?time=602

添加视频中显示的弹出按钮后,我创建了一个 IBAction 函数,如下所示:

@IBAction func onLanguageSelected(_ sender: Any?) {
    print("menu selection updated!")
}

然后,我将两个菜单项连接到这个 IBAction 函数,如下所示:

构建成功,在运行时,点击按钮后,应用程序崩溃,并抛出以下异常:

-[UIMenu establishInitialDefaultSingleSelection] 断言失败,UIMenu.m:535 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“菜单没有用于默认选择的有效元素”

为什么我会出现这个异常?这里需要修复什么?

【问题讨论】:

    标签: swift button interface-builder ios15 nspopupbutton


    【解决方案1】:

    您需要将菜单项添加到弹出按钮,例如:

    @IBOutlet weak var popUpButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupPopUpButton()
    }
    
    func setupPopUpButton() {
        let popUpButtonClosure = { (action: UIAction) in
            print("Pop-up action")
        }
                
        popUpButton.menu = UIMenu(children: [
            UIAction(title: "First Entry", handler: popUpButtonClosure),
            UIAction(title: "Second Entry", handler: popUpButtonClosure)
        ])
        popUpButton.showsMenuAsPrimaryAction = true
    }
    

    【讨论】:

    猜你喜欢
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 2012-12-04
    • 2017-09-27
    • 2019-09-12
    • 1970-01-01
    • 2014-10-13
    相关资源
    最近更新 更多