I’m trying to pop up a UIMenu
once the user press the correct UIBarButton
, but the problem is the UIMenu doesn’t pop up and I don’t know why, would be cool if you guys can help me with that.
-
I also added the comment code to show you all the tries.
-
My problem is when I set up the
UIBarButton
but not sure. BTW! i didn’t use computed properties due to the fact that I couldn’t edit them when I use that way, so I just used a normal stored variable.
myCode:
// 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
// }
//
2
Answers
Use "self" instead of the name of your view controller (NotificationsViewController) when you add the selector in your bar button initialization, or just refer to the function name directly if you don’t like putting self. in front of methods and properties when it’s not required.
You’re not using the proper initializer for the menu.