ViewController不符合Swift上的协议

时间:2014-10-15 15:23:35

标签: ios swift

我在班上打电话给代表时遇到了问题。我有一个SideBarTableController和SideBar.swift文件。它正常工作,直到我注销并重新进入。这是代码。它一直告诉我' ViewController'不符合协议' SideBarDelegate'。我是编程的新手,如果这很模糊,请提前对不起。什么都有帮助谢谢!

import UIKit
import iAd

class ViewController: UIViewController, SideBarDelegate {  -----This is where the error is happening


@IBOutlet var menuTab: UIButton!        // Menu Tab
@IBOutlet var businessButton: UIButton! // Business Button
@IBOutlet var adBanner: ADBannerView!   // Banner Ad
@IBOutlet var imageView: UIImageView!   // Main Image in middle
var topHeader: UIImageView!             // Utility header

var sideBar:SideBar = SideBar()         // Side Bar


override func viewDidLoad() {
    super.viewDidLoad()



    // Function for menu
   // menuTab.addTarget(self, action: "buttonPressed", forControlEvents: UIControlEvents.TouchUpInside)
    func menuTab(sender: UIButton){
        sideBar = SideBar(sourceView: self.view, menuItems: ["Home", "Business Directory", "Classifieds", "Featured News", "Jobs", "Restaurants", "Sports"])
        sideBar.delegate = self        }

    // Gives the screen dimensions
    let screenSize: CGRect = UIScreen.mainScreen().bounds
    let screenWidth = screenSize.width
    let screenHeight = screenSize.height


    // Side bar action and text
    sideBar = SideBar(sourceView: self.view, menuItems: ["Home", "Business Directory", "Classifieds", "Featured News", "Jobs", "Restaurants", "Sports"])
    sideBar.delegate = self


    // Utility Background
    var topHeader = UIView(frame: CGRectMake(0, 0, screenWidth, 17))
    topHeader.backgroundColor = UIColor(white: 0.0, alpha: 1.0)
    self.view.addSubview(topHeader)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// Side Bar funcion
func sideBarDidSelectButtonAtIndex(index: Int) {
    if index == 0{
        imageView.backgroundColor = UIColor.redColor()
        imageView.image = nil
    } else if index == 1{
        imageView.backgroundColor = UIColor.clearColor()
        imageView.image = UIImage(named: "image2")
    }
}
// Status bar style (white)
override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return UIStatusBarStyle.LightContent
}


}

这是我调用SideBarDelegate

的地方
import UIKit

@objc protocol SideBarDelegate{
    func sideBarDidSelectButtonAtIndex(index:Int)
    func sideBarWillClose()
    func sideBarWillOpen()
}

class SideBar: NSObject, SideBarTableViewControllerDelegate {

    let barWidth:CGFloat = 176.0
    let sideBarTableViewTopInset:CGFloat = 64.0
    let sideBarContainerView:UIView = UIView()
    let sideBarTableViewController:SideBarTableViewController = SideBarTableViewController()
    let originView:UIView!

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

    override init() {
        super.init()
    }

    init(sourceView:UIView, menuItems:Array<String>){
        super.init()
        originView = sourceView
        sideBarTableViewController.tableData = menuItems

        setupSideBar()

        animator = UIDynamicAnimator(referenceView: originView)

        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)


    }


    func setupSideBar(){

        sideBarContainerView.frame = CGRectMake(-barWidth - 1, originView.frame.origin.y, barWidth, originView.frame.size.height)
        sideBarContainerView.backgroundColor = UIColor.clearColor()
        sideBarContainerView.clipsToBounds = false

        originView.addSubview(sideBarContainerView)

        let blurView:UIVisualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.Light))
        blurView.frame = sideBarContainerView.bounds
        sideBarContainerView.addSubview(blurView)


        sideBarTableViewController.delegate = self
        sideBarTableViewController.tableView.frame = sideBarContainerView.bounds
        sideBarTableViewController.tableView.clipsToBounds = false
        sideBarTableViewController.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        sideBarTableViewController.tableView.backgroundColor = UIColor.clearColor()
        sideBarTableViewController.tableView.scrollsToTop  = false
        sideBarTableViewController.tableView.contentInset = UIEdgeInsetsMake(sideBarTableViewTopInset, 0, 0, 0)

        sideBarTableViewController.tableView.reloadData()

        sideBarContainerView.addSubview(sideBarTableViewController.tableView)

    }


    func handleSwipe(recognizer:UISwipeGestureRecognizer){
        if recognizer.direction == UISwipeGestureRecognizerDirection.Left{
            showSideBar(false)
            delegate?.sideBarWillClose()

        }else{
            showSideBar(true)
            delegate?.sideBarWillOpen()
        }

    }


    func showSideBar(shouldOpen:Bool){
        animator.removeAllBehaviors()
        isSideBarOpen = shouldOpen

        let gravityX:CGFloat = (shouldOpen) ? 0.5 : -0.5
        let magnitude:CGFloat = (shouldOpen) ? 20 : -20
        let boundaryX:CGFloat = (shouldOpen) ? barWidth : -barWidth - 1


        let gravityBehavior:UIGravityBehavior = UIGravityBehavior(items: [sideBarContainerView])
        gravityBehavior.gravityDirection = CGVectorMake(gravityX, 0)
        animator.addBehavior(gravityBehavior)

        let collisionBehavior:UICollisionBehavior = UICollisionBehavior(items: [sideBarContainerView])
        collisionBehavior.addBoundaryWithIdentifier("sideBarBoundary", fromPoint: CGPointMake(boundaryX, 20), toPoint: CGPointMake(boundaryX, originView.frame.size.height))
        animator.addBehavior(collisionBehavior)

        let pushBehavior:UIPushBehavior = UIPushBehavior(items: [sideBarContainerView], mode: UIPushBehaviorMode.Instantaneous)
        pushBehavior.magnitude = magnitude
        animator.addBehavior(pushBehavior)


        let sideBarBehavior:UIDynamicItemBehavior = UIDynamicItemBehavior(items: [sideBarContainerView])
        sideBarBehavior.elasticity = 0.3
        animator.addBehavior(sideBarBehavior)

    }

    func sideBarControlDidSelectRow(indexPath: NSIndexPath) {
        delegate?.sideBarDidSelectButtonAtIndex(indexPath.row)
    }

}

1 个答案:

答案 0 :(得分:8)

您的SideBarDelegate协议指定了必须实现的一些方法才能符合该协议。因此,请查看该协议的定义,确定其中定义的方法,然后在ViewController类中实现它们。

您可能错过了该协议中定义的某个功能,或者您在sideBarDidSelectButtonAtIndex的定义中输入了错误的内容。如果没有看到这个协议的定义,我们不能说。


更新:

您随后提供了协议的定义。它定义了您尚未在视图控制器中实现的两个附加功能:

func sideBarWillClose()
func sideBarWillOpen()

您还必须在视图控制器中实现这两个功能(即使您没有放入任何内容)。