0

UIMenu一旦用户按下正确的,我试图弹出一个UIBarButton,但问题是 UIMenu 没有弹出,我不知道为什么,如果你们能帮我解决这个问题会很酷。

  • 我还添加了注释代码以向您展示所有尝试。

  • 我的问题是当我设置UIBarButton但不确定时。顺便提一句!我没有使用计算属性,因为当我使用这种方式时我无法编辑它们,所以我只使用了一个普通的存储变量。

我的代码:

// MARK: - UIBarButtonItem Creation and Configuration
    
     func configureUIButtons() {
        
//        let toolbarButtonItems = [
//            customTitleBarButtonItem,
//            flexibleSpaceBarButtonItem,
//            deleteBarButtonItem
//        ]
        
        let toolbarButtonItems = [
            markBarButtonItem, flexibleSpaceBarButtonItem, deleteBarButtonItem
        ]
        
        toolBar.setItems(toolbarButtonItems, animated: true)
    }
    
    
    
    var deleteBarButtonItem: UIBarButtonItem = UIBarButtonItem(title: "Delete", style: .plain, target: self, action: #selector(NotificationsViewController.deleteButtonItemClicked(_:)))
    
    
    
//    var deleteBarButtonItem: UIBarButtonItem {
//        let deleteBarButtonItem: UIBarButtonItem = UIBarButtonItem(title: "Delete", style: .plain, target: self, action: #selector(NotificationsViewController.deleteButtonItemClicked(_:)));
//
//        return deleteBarButtonItem
//
//    }

    
    var flexibleSpaceBarButtonItem: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    
    
//    var flexibleSpaceBarButtonItem: UIBarButtonItem {
//        return UIBarButtonItem(barButtonSystemItem: .flexibleSpace,
//                               target: nil,
//                               action: nil)
//    }
    
    
    
    var markBarButtonItem: UIBarButtonItem = UIBarButtonItem(title: "Mark", style: .plain, target: self, action: #selector(NotificationsViewController.markButtonItemClicked(_:)))
    
    
    
    // MARK: - Actions
    @objc
    func deleteButtonItemClicked(_ barButtonItem: UIBarButtonItem) {
        print("Delete Button is clicked \(barButtonItem).")
    }

    
    @objc
    func markButtonItemClicked(_ barButtonItem: UIBarButtonItem ) -> UIMenu {
        print("Mark is pressed")


        let markMenu = UIMenu(title: "", children: [
                    UIAction(title: "Flag", image: UIImage(systemName: "flag")) { action in
                        print("Flag is pressed")
                    },
                    UIAction(title: "Mark as Unread", image: UIImage(systemName: "envelope.badge")) { action in
                        print("Mark as Unread is pressed")
                    },
                ])

                return markMenu

    }
    
    
//    func menuItems() -> UIMenu {
//        let markMenu = UIMenu(title: "", children: [
//            UIAction(title: "Flag", image: UIImage(systemName: "flag")) { action in
//                print("Flag is pressed")
//            },
//            UIAction(title: "Mark as Unread", image: UIImage(systemName: "envelope.badge")) { action in
//                print("Mark as Unread is pressed")
//            },
//        ])
//
//        return markMenu
//    }
//
    
4

2 回答 2

1

您没有为菜单使用正确的初始化程序。

let markMenu = UIMenu(title: "", children: [
    UIAction(title: "Flag", image: UIImage(systemName: "flag")) { action in
        print("Flag is pressed")
    },
    UIAction(title: "Mark as Unread", image: UIImage(systemName: "envelope.badge")) { action in
        print("Mark as Unread is pressed")
    },
])
let markBarButtonItem = UIBarButtonItem(title: "Mark", image: nil, primaryAction: nil, menu: markMenu)
let toolBar = UIToolbar(frame: CGRect(origin: .zero, size: CGSize(width: 300, height: 100)))
toolBar.setItems([markBarButtonItem], animated: true)
于 2021-01-05T09:32:16.150 回答
0

在 bar 按钮初始化中添加选择器时,使用“self”而不是视图控制器的名称(NotificationsViewController),或者如果您不喜欢放置 self.,请直接​​引用函数名称。不需要时放在方法和属性的前面。

于 2021-01-04T20:39:15.087 回答