我試圖對 UIKit 中的選單項使用 UIAction 中的操作。所以,對于第一個按鈕,我無法應用操作。它向我顯示錯誤“在范圍內找不到'動作'”
在這種情況下,我真的很想使用選擇器。我想知道什么是對選擇器采取行動的完美方式
class MessageViewController : UIViewController, UITableViewDelegate {
private lazy var first = UIAction(title: "Edit", image: UIImage(systemName: "pencil.circle"), attributes: [], state: .off) { [self]_ in
action: #selector(self.RightSideBarButtonItemTapped(_:))
}
private lazy var second = UIAction(title: "Second", image: UIImage(systemName: "pencil.circle"), attributes: [.destructive], state: .on) { action in
print("Second")
#selector(self.sendMessageRightSideBarButtonItemTapped(_:))
}
private lazy var third = UIAction(title: "Third", image: UIImage(systemName: "pencil.circle"), attributes: [], state: .off) { action in
print("third")
}
private lazy var elements: [UIAction] = [first]
private lazy var menu = UIMenu(title: "new", children: elements)
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: nil)
menu = menu.replacingChildren([first, second, third])
if #available(iOS 14.0, *) {
navigationItem.rightBarButtonItem?.menu = menu
}
}
來自@Hangar Rash 的嘗試解決方案
private lazy var first = UIAction(title: "Edit", image: UIImage(systemName: "pencil.circle"), attributes: [], state: .off)
{ [unowned self] action in
self.RightSideBarButtonItemTapped(_:)
// Getting error on this line which says its " Function is unused "
}
override func viewDidLoad() {}
override func viewDidAppear(_ animated: Bool) {}
@objc func RightSideBarButtonItemTapped(_ sender:UIBarButtonItem!)
{
let vc = IceWorldView()
present(vc, animated: true)
}
uj5u.com熱心網友回復:
您的第一個操作有兩個問題:
- 語法錯誤
action - 沒有必要使用
#selector. 直接呼叫方法就好了
改變:
private lazy var first = UIAction(title: "Edit", image: UIImage(systemName: "pencil.circle"), attributes: [], state: .off) { [self]_ in
action: #selector(self.RightSideBarButtonItemTapped(_:))
}
至:
private lazy var first = UIAction(title: "Edit", image: UIImage(systemName: "pencil.circle"), attributes: [], state: .off) { [unowned self] action in
self.RightSideBarButtonItemTapped(someButton)
}
您RightSideBarButtonItemTapped需要一個UIBarButtonItem引數,但選單中沒有引數。您可以制作一個虛擬按鈕實體來傳遞,也可以更改RightSideBarButtonItemTapped它,使其不帶任何引數。無論如何,您似乎都沒有使用傳入的發件人。
修復#selector在你的第二個動作中的使用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/530305.html
上一篇:CodeIgniter4:如何在發送HTTP請求和接收HTTP回應之間執行MySQL查詢
下一篇:我想要一個按空格進行分詞的查詢
