在 Swift 中加载 UIView 时自定义转换出错

Error with custom transition on loading UIView in Swift

我有一个包含多个元素的 UiViewcontroller。单击单元格上的 accessoryButtonType 时,它​​应该打开带有自定义转换的 window,但是在创建转换代码后,我的 UIViewController 根本不会加载。

通过添加断点,它在我的 viewDidLoad 方法后崩溃;

这是我的 viewController 脚本的第一部分及其 viewDidLoad 方法

class jeans: UIViewController, UITableViewDelegate{

@IBOutlet var insertion: UITableView!
@IBOutlet var continent: UITableView!
@IBOutlet var sizeOutputCell: UITableView!
var size = ["Size waist", "Size inseam"]
let customTransitionManager = TransitionManager()

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor(red:(239/255.0), green:(238/255.0), blue:(243/255.0), alpha: 1.0)
    insertion.scrollEnabled = false;
    continent.scrollEnabled = false;


    let nib = UINib(nibName: "vwTableCell", bundle: nil)
    self.insertion.registerNib(nib, forCellReuseIdentifier: "customCell")
    let outputNib = UINib(nibName:"outputView", bundle:nil)
    self.sizeOutputCell.registerNib(outputNib, forCellReuseIdentifier: "outputCell")
    self.customTransitionManager.sourceViewController = self

}

这行代码在我的 TransitionManager.Class 中突出显示(第 128 行):

let menuViewController = !self.presenting ? screens.from as! DetailedView : screens.to as! DetailedView

出现以下错误:

Could not cast value of type 'Converter.ViewController' (0x1000f7ce0) to 'Clothes_Converter.DetailedView' (0x1000f7f10).

这是它所在的方法:

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

    // get reference to our fromView, toView and the container view that we should perform the transition in
    let container = transitionContext.containerView()

    // create a tuple of our screens
    let screens : (from:UIViewController, to:UIViewController) = (transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!, transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!)

    // assign references to our menu view controller and the 'bottom' view controller from the tuple
    // remember that our menuViewController will alternate between the from and to view controller depending if we're presenting or dismissing
    let menuViewController = !self.presenting ? screens.from as! DetailedView : screens.to as! DetailedView
    let topViewController = !self.presenting ? screens.to as UIViewController : screens.from as UIViewController

    let menuView = menuViewController.view
    let topView = topViewController.view

    // prepare menu items to slide in
    if (self.presenting){
        self.offStageMenuControllerInteractive(menuViewController) // offstage for interactive
    }

    // add the both views to our view controller

    container.addSubview(menuView)
    container.addSubview(topView)
    container.addSubview(self.statusBarBackground)

    let duration = self.transitionDuration(transitionContext)

    // perform the animation!
    UIView.animateWithDuration(duration, delay: 0.0, options: nil, animations: {

        if (self.presenting){
            self.onStageMenuController(menuViewController) // onstage items: slide in
            topView.transform = CGAffineTransformMakeTranslation(-container.frame.width, 0)
        }
        else {
            topView.transform = CGAffineTransformIdentity
            self.offStageMenuControllerInteractive(menuViewController)
        }

        }, completion: { finished in

            // tell our transitionContext object that we've finished animating
            if(transitionContext.transitionWasCancelled()){

                transitionContext.completeTransition(false)
                // bug: we have to manually add our 'to view' back http://openradar.appspot.com/radar?id=5320103646199808
                UIApplication.sharedApplication().keyWindow!.addSubview(screens.from.view)

            }
            else {

                transitionContext.completeTransition(true)
                // bug: we have to manually add our 'to view' back http://openradar.appspot.com/radar?id=5320103646199808
                UIApplication.sharedApplication().keyWindow!.addSubview(screens.to.view)

            }
            UIApplication.sharedApplication().keyWindow!.addSubview(self.statusBarBackground)

    })

}

我该如何解决这个问题才能正常工作?我试图缩小崩溃的范围,但在我的 viewDidLoad 中添加了一个断点(最后)然后进入所有导致 xCode 崩溃的地方(因为它太多了)我还在每个方法之前添加了断点。

如果出于任何原因您需要完整的代码,可以在 github~ 上找到它: https://github.com/bbriann123/Clothes/tree/master/Clothes_Converter/Clothes%20Converter

如有任何帮助,我们将不胜感激。

DetailedView 不是 ViewController,但您强迫它成为一个。

as! DetailedView中的!改成as? DetailedView,剩下的自己说了算

这是代码

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

    // get reference to our fromView, toView and the container view that we should perform the transition in
    let container = transitionContext.containerView()

    // create a tuple of our screens
    let screens : (from:UIViewController, to:UIViewController) = (transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!, transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!)

    // assign references to our menu view controller and the 'bottom' view controller from the tuple
    // remember that our menuViewController will alternate between the from and to view controller depending if we're presenting or dismissing
    //Changed the ! to ?
    let menuViewController = !self.presenting ? screens.from as? DetailedView : screens.to as? DetailedView
    let topViewController = !self.presenting ? screens.to as UIViewController : screens.from as UIViewController
    let menuView = menuViewController?.view
    let topView = topViewController.view
    // prepare menu items to slide in
    if (self.presenting){
        //self.offStageMenuControllerInteractive(menuViewController) // offstage for interactive
    }

    // add the both views to our view controller

    container.addSubview(menuView!)
    container.addSubview(topView)

    //THROWS unexpected nil value while unwrapping.
    //container.addSubview(self.statusBarBackground)

    let duration = self.transitionDuration(transitionContext)
    // perform the animation!
    UIView.animateWithDuration(duration, delay: 0.0, options: .CurveEaseInOut , animations: {
        if (self.presenting){
            self.onStageMenuController(menuViewController!) // onstage items: slide in
            topView.transform = CGAffineTransformMakeTranslation(-container.frame.width, 0)
        }
        else {
            topView.transform = CGAffineTransformIdentity
            self.offStageMenuControllerInteractive(menuViewController!)
        }

        }, completion: { finished in

            // tell our transitionContext object that we've finished animating
            if(transitionContext.transitionWasCancelled()){

                transitionContext.completeTransition(false)
                // bug: we have to manually add our 'to view' back http://openradar.appspot.com/radar?id=5320103646199808
                UIApplication.sharedApplication().keyWindow!.addSubview(screens.from.view)

            }
            else {

                transitionContext.completeTransition(true)
                // bug: we have to manually add our 'to view' back http://openradar.appspot.com/radar?id=5320103646199808
                UIApplication.sharedApplication().keyWindow!.addSubview(screens.to.view)

            }
            //THROWS unexpected nil value while unwrapping.
           // UIApplication.sharedApplication().keyWindow!.addSubview(self.statusBarBackground)

    })

}