当我单击“+”时,我想在右侧 BarButtonItem 下显示菜单(弹出框),其中将有两个选项。按其中一个选项将导致其他视图控制器。
我正在使用 Xcode 10 和 Swift 4.2。
这是我现在的代码,没有任何反应。我做错了什么?我可以用不同的方式写作吗?
import Foundation
class RootVC: UITableViewController {
@IBOutlet weak var openSideMenu: UIBarButtonItem!
let itemArray = ["1", "2", "3"]
override func viewDidLoad() {
super.viewDidLoad()
openSideMenu.target = self.revealViewController()
openSideMenu.action = #selector(SWRevealViewController.revealToggle(_:))
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RootCell", for: indexPath)
cell.textLabel?.text = itemArray[indexPath.row]
return cell
}
@IBAction func addBarButtonPressed(_ sender: UIBarButtonItem) {
let menu = UIMenuController.shared
menu.menuItems =
[UIMenuItem(title: "Test me", action: Selector("deleteLine")),
UIMenuItem(title: "Test me", action: Selector("deleteLine")),
UIMenuItem(title: "Test me", action: Selector("deleteLine"))]
menu.setTargetRect((self.navigationItem.rightBarButtonItems?.first?.frame)!, in: self.view)
becomeFirstResponder()
menu.setMenuVisible(true, animated: true)
}
}
extension UIBarButtonItem {
var frame: CGRect? {
guard let view = self.value(forKey: "view") as? UIView else {
return nil
}
return view.frame
}
}