我该如何解决这个错误?错误是无法分配给属性"目标是一种方法"

时间:2017-03-24 19:21:24

标签: ios swift navigationbar

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

3 个答案:

答案 0 :(得分:3)

工作代码

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

答案 1 :(得分:2)

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

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

答案 2 :(得分:0)

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