gpt4 book ai didi

ios - NSInvalidArgumentException 原因 : 'Storyboard doesn' t contain a view controller with identifier 'CenterViewController' '

转载 作者:IT王子 更新时间:2023-10-29 05:46:00 26 4
gpt4 key购买 nike

错误:无法实例化 UIMainStoryboardFile 'Main' 的默认 View Controller - 可能未设置指定的入口点?

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

我不知道我的主 Storyboard 有什么问题。我正在构建自己的滑出式导航面板。

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
}

}

最佳答案

非常简单。

在您的主 Storyboard中,您没有 Storyboard ID 为“CenterViewController”的 ViewController

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

关于ios - NSInvalidArgumentException 原因 : 'Storyboard doesn' t contain a view controller with identifier 'CenterViewController' ',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27833574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com