NSInvalidArgumentException原因:'故事板不包含带有标识符'CenterViewController'的视图控制器

时间:2015-01-08 05:36:33

标签: ios swift

错误:无法为UIMainStoryboardFile实现默认视图控制器' Main' - 也许没有设定指定的入口点?

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard (<UIStoryboard: 0x7ff949770c30>) doesn't contain a view controller with identifier 'CenterViewController''

我不会&#39;知道我的主要故事板有什么问题。我正在构建自己的滑出式导航面板。

CenterViewController.swift

import UIKit
@objc
protocol CenterViewControllerDelegate {
    optional func toggleLeftPanel()
    optional func collapseSidePanels()
}

class CenterViewController: UIViewController, SideMenuPanelViewControllerDelegate {

//@IBOutlet weak private var testimageView: UIImageView!
@IBOutlet weak private var testLabel:UILabel!

var delegate: CenterViewControllerDelegate?

// MARK: Button actions

@IBAction func MenuTapped(sender: AnyObject) {
    delegate?.toggleLeftPanel?()
}

func localSelected(local: LocalMenus) {
    //imageView.image = animal.image
    testLabel.text = local.title

    delegate?.collapseSidePanels?()
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

ContainerViewController.swift

import UIKit
import QuartzCore

enum SideOutState {
    case BothCollapsed
    case LeftPanelExpanded
    case RightPanelExpanded
}

class ContainerViewController: UIViewController, CenterViewControllerDelegate, UIGestureRecognizerDelegate{
    var centerNavigationController: UINavigationController!
    var centerViewController: CenterViewController!

    var currentState: SideOutState = .BothCollapsed {
        didSet {
            let shouldShowShadow = currentState != .BothCollapsed
            showShadowForCenterViewController(shouldShowShadow)
        }
    }

var leftMenuController: SideMenuPanelViewController?

let centerPanelExpandedOffset: CGFloat = 60

override func viewDidLoad() {
    super.viewDidLoad()

    centerViewController = UIStoryboard.centerViewController()
    centerViewController.delegate = self

    // wrap the centerViewController in a navigation controller, so we can push views to it
    // and display bar button items in the navigation bar
    centerNavigationController = UINavigationController(rootViewController: centerViewController)
    view.addSubview(centerNavigationController.view)
    addChildViewController(centerNavigationController)

    centerNavigationController.didMoveToParentViewController(self)

    let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")
    centerNavigationController.view.addGestureRecognizer(panGestureRecognizer)
}

// MARK: CenterViewController delegate methods

func toggleLeftPanel() {
    let notAlreadyExpanded = (currentState != .LeftPanelExpanded)

    if notAlreadyExpanded {
        addLeftPanelViewController()
    }

    animateLeftPanel(shouldExpand: notAlreadyExpanded)
}

func collapseSidePanels() {
    switch (currentState) {
    case .LeftPanelExpanded:
        toggleLeftPanel()
    default:
        break
    }
}

func addLeftPanelViewController() {
    if (leftMenuController == nil) {
        leftMenuController = UIStoryboard.leftMenuController()
        leftMenuController!.local = LocalMenus.allLocal()

        addChildSidePanelController(leftMenuController!)
    }
}

func addChildSidePanelController(sidePanelController:SideMenuPanelViewController) {

    sidePanelController.delegate = centerViewController

    view.insertSubview(sidePanelController.view, atIndex: 0)

    addChildViewController(sidePanelController)
    sidePanelController.didMoveToParentViewController(self)
}

func animateLeftPanel(#shouldExpand: Bool) {
    if (shouldExpand) {
        currentState = .LeftPanelExpanded

        animateCenterPanelXPosition(targetPosition: CGRectGetWidth(centerNavigationController.view.frame) - centerPanelExpandedOffset)
    } else {
        animateCenterPanelXPosition(targetPosition: 0) { finished in
            self.currentState = .BothCollapsed

            self.leftMenuController!.view.removeFromSuperview()
            self.leftMenuController = nil;
        }
    }
}

func animateCenterPanelXPosition(#targetPosition: CGFloat, completion: ((Bool) -> Void)! = nil) {
    UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .CurveEaseInOut, animations: {
        self.centerNavigationController.view.frame.origin.x = targetPosition
        }, completion: completion)
}

func showShadowForCenterViewController(shouldShowShadow: Bool) {
    if (shouldShowShadow) {
        centerNavigationController.view.layer.shadowOpacity = 0.8
    } else {
        centerNavigationController.view.layer.shadowOpacity = 0.0
    }
}

// MARK: Gesture recognizer

func handlePanGesture(recognizer: UIPanGestureRecognizer) {
    // we can determine whether the user is revealing the left or right
    // panel by looking at the velocity of the gesture
    let gestureIsDraggingFromLeftToRight = (recognizer.velocityInView(view).x > 0)

    switch(recognizer.state) {
    case .Began:
        if (currentState == .BothCollapsed) {
            // If the user starts panning, and neither panel is visible
            // then show the correct panel based on the pan direction

            if (gestureIsDraggingFromLeftToRight) {
                addLeftPanelViewController()
            }

            showShadowForCenterViewController(true)
        }
    case .Changed:
        // If the user is already panning, translate the center view controller's
        // view by the amount that the user has panned
        recognizer.view!.center.x = recognizer.view!.center.x + recognizer.translationInView(view).x
        recognizer.setTranslation(CGPointZero, inView: view)
    case .Ended:
        // When the pan ends, check whether the left or right view controller is visible
        if (leftMenuController != nil) {
            // animate the side panel open or closed based on whether the view has moved more or less than halfway
            let hasMovedGreaterThanHalfway = recognizer.view!.center.x > view.bounds.size.width
            animateLeftPanel(shouldExpand: hasMovedGreaterThanHalfway)
        }
    default:
        break
    }
}

}

private extension UIStoryboard {
    class func mainStoryboard() -> UIStoryboard {
        return UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
    }

    class func leftMenuController() -> SideMenuPanelViewController? {
        return mainStoryboard().instantiateViewControllerWithIdentifier("SideMenuPanelViewController") as? SideMenuPanelViewController
    }

class func centerViewController() -> CenterViewController? {
    return mainStoryboard().instantiateViewControllerWithIdentifier("CenterViewController") as? CenterViewController
}

}

2 个答案:

答案 0 :(得分:18)

很简单。

在您的主故事板中,您没有带有故事板ID“CenterViewController”的ViewController

看这里: https://stackoverflow.com/a/11604827/3324388

答案 1 :(得分:8)

Xcode 8.0以后的简短回答,

转到Main.storyboard选择目标ViewController,按Command+option+3显示属性

  • 填写StoryBoard ID输入字段
  • 选中Use Storyboard ID复选框

enter image description here

我知道这可以在重复的参考答案中找到,但你需要阅读,尝试和错误等。