Ios 在Swift中加载UIView时自定义转换出错
我有一个UiViewcontroller,它有多个元素。当单击单元格上的accessoryButtonType时,它应该会打开一个带有自定义转换的窗口,但是在为转换创建代码之后,我的UIViewController根本不会加载 通过添加断点,它在我的viewDidLoad方法之后崩溃 下面是我的viewController脚本的第一部分及其viewDidLoad方法Ios 在Swift中加载UIView时自定义转换出错,ios,swift,uiviewcontroller,transition,Ios,Swift,Uiviewcontroller,Transition,我有一个UiViewcontroller,它有多个元素。当单击单元格上的accessoryButtonType时,它应该会打开一个带有自定义转换的窗口,但是在为转换创建代码之后,我的UIViewController根本不会加载 通过添加断点,它在我的viewDidLoad方法之后崩溃 下面是我的viewController脚本的第一部分及其viewDidLoad方法 class jeans: UIViewController, UITableViewDelegate{ @IBOutlet va
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行):
出现以下错误:
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~:
任何帮助都将不胜感激。DetailedView不是一个ViewController,但您正在强制它成为一个 更改
代码>在中作为!详细视图
到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)
})
}
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)
})
}