在 UIAction 中对 UIKit 中的菜单使用带有选择器的操作

Use action with selector in UIAction for menus in UIKit

提问人:Mai pa 提问时间:11/4/2022 最后编辑:Mai pa 更新时间:11/4/2022 访问量:756

问:

我尝试在UIAction中为UIKit中的菜单项使用操作。因此,对于第一个按钮,我无法应用操作。它显示错误“在范围内找不到'操作'”

在这种情况下,我真的很想使用选择器。我想知道在选择器上执行操作的完美方法是什么

 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疹的尝试解决方案

       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)
        }
Swift iPhone UIViein控制器 UIKitUp

评论

0赞 HangarRash 11/4/2022
for 有一个错别字。让它像其他两个一样。UIActionfirst
0赞 Mai pa 11/4/2022
' private lazy var first = UIAction(title: “First”, image: UIImage(systemName: “pencil.circle”), attributes: [.destructive], state: .on) { action in print(“First”) #selector(self.sendMessageRightSideBarButtonItemTapped(_:)) }'
0赞 Mai pa 11/4/2022
这不起作用

答:

1赞 HangarRash 11/4/2022 #1

您的第一个操作有两个问题:

  1. 语法错误action
  2. 没有必要使用 .直接调用方法即可#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)
}

您需要一个参数,但菜单中没有参数。您可以创建一个虚拟按钮实例来传入,也可以更改它,使其不采用任何参数。无论如何,您似乎不使用传入的发件人。RightSideBarButtonItemTappedUIBarButtonItemRightSideBarButtonItemTapped

在第二个操作中也修复了 的使用。#selector

评论

0赞 Mai pa 11/4/2022
使用相同的语法。函数出错。函数未使用
0赞 HangarRash 11/4/2022
通过在问题末尾添加您最近的尝试来编辑您的问题,并显示确切的错误。
0赞 Mai pa 11/4/2022
我已经修改了关于您的解决方案的问题
0赞 HangarRash 11/4/2022
正如我所说,您需要调用该方法,就像从其他任何地方调用它一样。这意味着括号必须包含方法所需的正确参数值。self.RightSideBarButtonItemTapped
0赞 HangarRash 11/4/2022
我刚刚更新了我的答案,提供了更多细节。