属性'self.animator'未在super.init调用中初始化

时间:2015-12-17 06:08:17

标签: error-handling init swift2.1

import UIKit

@objc protocol SideBarDelegate{

      func sideBarDidSelectButtonAtIndex(Index:Int)

       optional func sideBarWillClose()
       optional func sideBarWillOpen()
}


class SideBar: NSObject,SideBarTableViewControllerDelegate {

       let barWidth:CGFloat = 150
       let sideBarTableViewTopInset:CGFloat = 64
       let sideBarContainerView:UIView = UIView()
       let sideBarTableViewController:SideBarTableViewController = SideBarTableViewController()
       var originView:UIView?

       var animator:UIDynamicAnimator
       var delegate:SideBarDelegate
       var isSideBarOpen:Bool = false

    override init() {

        super.init()    //ERROR ON THIS LINE
    }

    init(sourceView:UIView, menuItems:Array<String>) {


           super.init()        //ERROR ON THIS LINE
           originView = sourceView
           sideBarTableViewController.tableData = menuItems
           animator = UIDynamicAnimator(referenceView: originView!)

           setupSideBar()



             let showGestureRecognizer:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
        showGestureRecognizer.direction = UISwipeGestureRecognizerDirection.Right
        originView!.addGestureRecognizer(showGestureRecognizer)

             let hideGestureRecognizer:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
        hideGestureRecognizer.direction = UISwipeGestureRecognizerDirection.Left
        originView!.addGestureRecognizer(hideGestureRecognizer)
    }
} 

我已经实施了更多方法,但错误是在这些代码行中

1 个答案:

答案 0 :(得分:0)

也许没有必要覆盖init()。或者您将init(sourceView:UIView, menuItems:Array<String>)声明为方便,如下所示:

convenience init(sourceView:UIView, menuItems:Array<String>) {
           super.init()
           self.init() // don't forget calling self.init
           originView = sourceView
           sideBarTableViewController.tableData = menuItems
           animator = UIDynamicAnimator(referenceView: originView!)
           setupSideBar()