ios - 无法识别侧边栏实例的选择器

标签 ios swift uiviewcontroller menu sidebar

我问这个问题是因为我在这个问题中收到的答案:How Do I Initialize Two Instances of NSObject in the same ViewController - Swift

把我带向了这个方向。无论我子类化为 NSObject 还是 UIViewController,当我将代码更改为以下内容时,我仍然会收到无法识别的选择器。

我仍在尝试创建左侧和右侧侧边栏。然而,我现在甚至无法加载一个侧边栏。我收到错误 [UIViewController Center]:无法识别的选择器发送到实例 xxxxx。

这个问题与我见过的其他无法识别的选择器实例问题不同,因为我不处理按钮,也没有导出,因为一切都是以编程方式完成的。因此,我无法指定链接到 Storyboard 中的 UIViewController 的子类。

我觉得一旦解决了选择器问题,代码就会起作用。由于代码现在是这样,应用程序编译得很好。问题是我收到的运行时错误。如果有必要,我可以提供来自调试器的信息。

Fwiw,运行时似乎从错误消息中识别出我有一个中心、左侧和右侧 View Controller 。

我没有包含 RightSideBar 代码,因为我认为如果左侧 Bar 运行,RightSideBar 将在我添加解决方案时运行。我希望您必须阅读的代码尽可能简短。

最后要注意的是,我即将完成所有打印报表。我实际上打印到了“我应该显示侧边栏”的位置。

这是侧边栏的代码:

//optional delegate methods that select when the sidebar opens and closes.
@objc protocol SideBarDelegate : class {

func sideBarDidSelectButtonAtIndex (itemIndex: Int)
optional func sideBarWillClose()
optional func sideBarWillOpen()

}

//this class sets up the actual sidebar.
class SideBar: UIViewController, SidebarTableViewControllerDelegate {


//width of the bar, tableview setup, and views for the sidebar
let barWidth:CGFloat = 175.0
let sideBarTableViewTopInset:CGFloat = 25.0
let sideBarContainerView:UIViewController = UIViewController()
let sideBarTableViewController:SidebarTableViewController = SidebarTableViewController()
var originView:UIViewController?

//var for dynamic effect and controlling the sidebar
var animator:UIDynamicAnimator!
weak var delegate:SideBarDelegate?
var isSideBarOpen:Bool = false

//initializer for the "SideBar" class.
required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override init(nibName NibNameOrNil:String!, bundle nibBundleOrNil:NSBundle!) {
    super.init(nibName: nil, bundle: nil)

}

convenience init(){
    self.init(nibName: nil, bundle: nil)
}

//initializer for the tableView of menu items.
init(sourceView: UIViewController, menuItems: Array<String>, menuImages: [UIImage]){

    self.originView = sourceView
    self.sideBarTableViewController.tableData = menuItems
    self.sideBarTableViewController.imageData = menuImages

    println("set initialization values")
    super.init(nibName: nil, bundle: nil)

    //initializing the views and animation for the menu.
    setupSideBar()
    animator = UIDynamicAnimator(referenceView: originView!.view)

    println("finished initialization")

    //swipe gesture recognition for opening the menu.
    let showGestureRecognizer:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
    showGestureRecognizer.direction = UISwipeGestureRecognizerDirection.Right
    originView!.view.addGestureRecognizer(showGestureRecognizer)

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

override func viewDidLoad() {

    println("view loaded")


}

//this function handles the direction of swipes
func handleSwipe(recognizer: UISwipeGestureRecognizer){
    if recognizer.direction == UISwipeGestureRecognizerDirection.Left {
        showSideBar(false)
        delegate?.sideBarWillClose?()
        println("closed the sideBar")
    } else {
        println("opened the sideBar")
        showSideBar(true)
        delegate?.sideBarWillOpen?()
    }
}

//function for setting up the sidebar.
func setupSideBar () {

    println("setup sideBar")

    //setting up the frame/outline of the side bar.

    sideBarContainerView.view.frame = CGRectMake(-barWidth - 1, originView!.view.frame.origin.y, barWidth, originView!.view.frame.size.height)

    //setting up the color of the sidebar.
    sideBarContainerView.view.backgroundColor = UIColor.clearColor()

    //disables subviews from being confined to the sidebar.
    sideBarContainerView.view.clipsToBounds = false

    //placing the sidebar in the UIView
    originView!.view.addSubview(sideBarContainerView.view)

    //adding blur to the menu.
    let blurView:UIVisualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.Light))
    blurView.frame = sideBarContainerView.view.bounds
    sideBarContainerView.view.addSubview(blurView)


    //setting up controls for the sidebar
    sideBarTableViewController.delegate = self
    sideBarTableViewController.tableView.frame = sideBarContainerView.view.bounds
    sideBarTableViewController.tableView.clipsToBounds = false

    //disabling the scroll feature. Delete to keep the scroll feature.
    sideBarTableViewController.tableView.scrollsToTop = false

    //This will remove separators in the UITableCell. Delete to keep separators.
    sideBarTableViewController.tableView.separatorStyle = UITableViewCellSeparatorStyle.None

    //This sets the background color of the sidebar and creates the inset.
    sideBarTableViewController.tableView.backgroundColor = UIColor.clearColor()
    sideBarTableViewController.tableView.contentInset = UIEdgeInsets(top: sideBarTableViewTopInset, left: 0, bottom: 0, right: 0)

    //reloads the sidebar and adds the container view to the sideBarTableViewController.
    sideBarTableViewController.tableView.reloadData()
    sideBarContainerView.view.addSubview(sideBarTableViewController.tableView)
    originView?.addChildViewController(sideBarContainerView)
    sideBarContainerView.didMoveToParentViewController(originView)

}


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

    println("I should be showing the sideBar")

    //simple if and else statements to define the direction of animation and intensity of animation
    let gravityX:CGFloat = (shouldOpen) ? 0.5 : -0.5
    let magnitude:CGFloat = (shouldOpen) ? 20 : -20
    let boundaryX:CGFloat = (shouldOpen) ? barWidth : -barWidth - 1

    //controls the behavior of the animation.
    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!.view.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)
}

}

这是主视图 Controller :

class Home: UIViewController, SideBarDelegate {

//*** Must put logout code into the logout button it should log the user out if they press it ***

var sideBar:SideBar = SideBar()

var homeImage = UIImage(named: "Shine Home")
var profileImage = UIImage(named: "Shine Profile")
var shareImage = UIImage(named: "Shine Share")
var aboutImage = UIImage(named: "Shine About")
var helpImage = UIImage(named: "Shine Help")

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    //setting up the menu items for the sidebar.
    sideBar = SideBar(sourceView: self, menuItems: ["Home", "Profile", "Share", "About", "Help"], menuImages: [homeImage!, profileImage!, shareImage!, aboutImage!, helpImage!])

    sideBar.delegate = self


}

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


func sideBarDidSelectButtonAtIndex(itemIndex: Int) {

    switch itemIndex {

    case 0:
        let vc = storyboard?.instantiateViewControllerWithIdentifier("Home") as! Home
        self.navigationController?.pushViewController(vc, animated: true)
    case 1:
        performSegueWithIdentifier("profile", sender: self)
    case 2:
        performSegueWithIdentifier("share", sender: self)
    case 3:
        performSegueWithIdentifier("about", sender: self)
    case 4:
        performSegueWithIdentifier("help", sender: self)

    default:
        break
    }
}

/*
// 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.
}
*/

}

最佳答案

这种无法识别选择器的情况源于运行时不知道您要激活哪个特定对象。

运行时看到 sideBarContainerView 对象,这是编译器可以接受的。

问题是您的动画行为是针对 View 的。您的 UIViewController 对象尚未下降到 UIViews 中。您需要将 .view 添加到 showSideBar 函数中的所有 sideBarContainerView 中。

如果您分解函数并仅使用 isSideBarOpen 作为常量中 if 语句的 bool 值并将其放置在菜单的初始化中,那么您将立即收到选择器错误。

这只是指出错误已经超过最后一个打印语句的第二种方法“我应该显示 SideBar

如果你这样做,那么你的菜单就会加载。到目前为止,我还没有解决方案可以将两个菜单初始化为左侧菜单和右侧菜单。

关于ios - 无法识别侧边栏实例的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31954566/

相关文章:

objective-c - UISlider 在最大边缘位置不响应

iphone - 跨多个 UIViewController 重用 Nib

ios - 降级应用内级别返回错误

ios - 火力地堡, swift : Not able to retrieve Data

ios - 优化 - 步进可能表现得很奇怪;变量在 Xcode 9.1 中可能不可用

ios - Instagram 风格的相机转换

ios - 字段 '___recordID' 未标记为可查询

ios - 使用 Xcode 6.1 将依赖项添加到存储库

ios - CLLocationManager 一段时间后未在后台接收更新

ios - 在 UIBezierPath 上绘制径向背景