Home > Net >  Use action with selector in UIAction for menus in UIKit
Use action with selector in UIAction for menus in UIKit

Time:11-05

Im trying to use action in the UIAction for my menu item in UIKit. So, For first button Im not able to apply action. It shows me error "Cannot find 'action' in scope"

I really want to use selector in this case. I want to know what's perfect way to take action on the selector

 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
                    }
    }

Tried Solution from @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)
        }

CodePudding user response:

Your first action has two issues:

  1. a syntax error with action
  2. there's no need to use #selector. Just call the method directly

Change:

private lazy var first = UIAction(title: "Edit", image: UIImage(systemName: "pencil.circle"), attributes: [], state: .off) { [self]_ in
    action:  #selector(self.RightSideBarButtonItemTapped(_:))
}

to:

private lazy var first = UIAction(title: "Edit", image: UIImage(systemName: "pencil.circle"), attributes: [], state: .off) { [unowned self] action in
    self.RightSideBarButtonItemTapped(someButton)
}

Your RightSideBarButtonItemTapped expects a UIBarButtonItem parameter but you don't have one in the menu. You can either made a dummy button instance to pass in or you can change the RightSideBarButtonItemTapped so it doesn't take any parameters. You don't seem to use the passed in sender anyway.

Fix the use of #selector in your second action as well.

  • Related