我该如何解决这个错误?错误是无法分配给属性“target is a method”

问题描述 投票:0回答:3

This is Error page image //这是我的自定义导航功能

    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)
        }
ios swift navigationbar
3个回答
2
投票

我想这就是你要找的东西,target和动作都是方法。你正在把它当成属性。我猜测self.revealViewController()返回SWRevealViewController实例。

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

3
投票

工作代码

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

0
投票

你没有使用条形按钮项,SWRevealController动作和目标工作与条形按钮而不是简单的按钮

© www.soinside.com 2019 - 2024. All rights reserved.