![](/img/trans.png)
[英]dismissViewControllerAnimated causes EXC_BAD_ACCESS in iOS 7.1 only
[英]dismissViewControllerAnimated causes EXC_BAD_ACCESS - swift
我試圖用dismissViewControllerAnimated(true,完成:{})來關閉UIViewController,但是嘗試執行時會得到EXC_BAD_ACCESS。 (也就是說,工作的十分之一)。
我使用了一個自定義的transitionDelegate,並且當我未設置它時,它就可以工作了。
ListTransitionDelegate返回動畫器和presentationController。
PresentationController看起來像這樣
init(presentingViewController: UIViewController!, presentedViewController: UIViewController!, controllerStyle: SideControllerStyle, shortest: CGFloat) {
self.controllerStyle = controllerStyle
self.shortest = shortest
super.init(presentingViewController: presentingViewController, presentedViewController: presentedViewController)
self.dimmingView.backgroundColor = UIColor.blackColor()
var tapGesture = UITapGestureRecognizer(target: self, action: Selector("closePresented"))
dimmingView.addGestureRecognizer(tapGesture)
}
override func adaptivePresentationStyle() -> UIModalPresentationStyle
{
return UIModalPresentationStyle.OverFullScreen
}
override func shouldPresentInFullscreen() -> Bool
{
return true
}
override func presentationTransitionWillBegin() {
var containerView = self.containerView
self.dimmingView.frame = self.containerView.bounds
containerView.insertSubview(self.dimmingView, atIndex:0)
presentedViewController.transitionCoordinator().animateAlongsideTransition({context in
self.dimmingView.alpha = 0.5
}, completion:nil)
}
override func presentationTransitionDidEnd(completed: Bool)
{
if !completed {
self.dimmingView.removeFromSuperview()
}
}
override func dismissalTransitionDidEnd(completed: Bool)
{
self.dimmingView.removeFromSuperview()
}
override func dismissalTransitionWillBegin() {
presentedViewController.transitionCoordinator().animateAlongsideTransition({context in
self.dimmingView.alpha = 0
}, completion: nil)
}
func closePresented() {
var presenting = self.presentingViewController
presentedViewController.dismissViewControllerAnimated(true, completion: nil)
}
override func sizeForChildContentContainer(container: UIContentContainer!, withParentContainerSize parentSize: CGSize) -> CGSize {
var size : CGSize
switch self.controllerStyle {
case .Left, .Right:
size = CGSizeMake(self.shortest, parentSize.height)
case .Bottom, .Top:
size = CGSizeMake(parentSize.width, self.shortest)
default:
size = CGSizeMake(parentSize.width, parentSize.height)
}
return size
}
override func frameOfPresentedViewInContainerView() -> CGRect {
var frame : CGRect
var size = sizeForChildContentContainer(nil, withParentContainerSize: containerView.bounds.size)
switch self.controllerStyle {
case .Left:
frame = CGRectMake(containerView.bounds.size.width - size.width, 0, size.width, size.height)
case .Right:
frame = CGRectMake(0, 0, size.width, size.height)
case .Bottom:
frame = CGRectMake(0, containerView.bounds.size.height - size.height, size.width, size.height)
case .Top:
frame = CGRectMake(0, 0, size.width, size.height)
default:
frame = CGRectMake(0, 0, size.width, size.height)
}
return frame
}
嘗試在方案設置中啟用僵屍對象。 (請記住在完成操作后將其關閉,因為該更改不會顯示在源代碼控制中,並且您確實不想在沒有ARC的情況下發布應用程序!)
如果收到錯誤消息
-[_UILayoutGuide superview]: message sent to deallocated instance 0x1781ac6a0
然后只需將此代碼添加到被關閉的視圖控制器中即可:
deinit {
self.view.removeFromSuperview()
}
UILayoutGuide
是一個私有的自動布局類。 我相信這是一個錯誤(並以此形式提交),因為為什么被解雇的視圖控制器需要自動布局? 從其父視圖中移除類將搶先阻止在自動布局約束被釋放后訪問它們。
斯威夫特1.1
嘗試使用完成參數代替nil
use {}
。 例如: presentedViewController.dismissViewControllerAnimated(true, completion: {})
在關閉viewController之前,應將TransitioningDelegate設置為nil。
我有同樣的問題。 我必須聲明類變量
class firstVC : UIViewController
{
let customTransitioninDelegate = customTransitioninDelegate().
...
//Then when created second view controller just assign it
-func xxx()
{
var secondVC = secondVC();
secondVC.transitioningDelegate = customTransitioninDelegate;
self.presentViewController(secondVC, animated: true, completion: nil);
}
}
class secondVC: UIViewController
{
...
func xxx()
{
if(self.presentingViewController != nil)
{
self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil);
}
}
}
並且解雇有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.