ios - 我该如何解决此错误?错误是无法分配给属性“目标是方法”-LMLPHP //这是我的自定义导航功能

    func addSlideMenuButton()

//这是我的自定义导航栏
      let navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width,height: 104))
 let backgroundImage = UIImage(named: "BarBa")?.resizableImage(withCapInsets: UIEdgeInsetsMake(0, 15, 0, 15), resizingMode: UIImageResizingMode.stretch)
                UINavigationBar.appearance().setBackgroundImage(backgroundImage, for: .default)
    // Create a navigation item with a title

let navigationItem = UINavigationItem()
                let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 70, height: 90 ))
                imageView.contentMode = .scaleAspectFit
                let image = UIImage(named: "logo.png")
                imageView.image = image
                navigationItem.titleView = imageView


                let button = UIButton.init(type: .custom)
                button.setImage(UIImage.init(named: "sidebar.png"), for: UIControlState.normal)
                button.addTarget(self, action:#selector(NewsTableViewController.callMethod), for: UIControlEvents.touchUpInside)
                button.frame = CGRect.init(x: 0, y: 0, width: 70, height: 80)
                let customBarItem = UIBarButtonItem(customView: button)
                self.navigationItem.leftBarButtonItem = customBarItem;
                navigationBar.items = [navigationItem]



                // Make the navigation bar a subview of the current view controller
                self.view.addSubview(navigationBar)



 //tapping the menu button work
 // Here revealViewController is a objective C class





   if self.revealViewController() != nil {
                button.target = self.revealViewController()
                button.action = #selector(SWRevealViewController.revealToggle(_:)) // in this 2 line i got error cannot assign to the property "target is a method"
                self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
                // for set customiz width
                self.revealViewController().rearViewRevealWidth = 250
            }
            // Assign the navigation item to the navigation bar
            navigationBar.items = [navigationItem]
            self.view.addSubview(navigationBar)
        }

最佳答案

我想这就是您要寻找的,target和action是方法。您将其视为属性。我猜self.revealViewController()返回SWRevealViewController实例。

button.addTarget(self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)), for: .touchUpInside)

关于ios - 我该如何解决此错误?错误是无法分配给属性“目标是方法”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43007567/

10-14 22:28
查看更多