jja*_*tie 7 uiviewcontroller ios swift uipresentationcontroller xcode8-beta2
我在Xcode beta 2和iOS 10 beta 2之前有一个工作的自定义UIPresentationController.我没有更改任何代码,但现在正在呈现标准模式演示文稿.
UIPresentationController的Apple示例代码中有一条说明:
对于将使用自定义演示控制器的演示文稿,该演示控制器也可以是transitioningDelegate.这避免了在源视图控制器中引入另一个对象或实现.
transitioningDelegate没有对其目标对象的强引用.为了防止在调用-presentViewController之前释放presentationController:animated:completion:NS_VALID_UNTIL_END_OF_SCOPE属性被附加到声明中.
我在演示之前和之后检查了呈现的视图控制器上的transitioningDelegate.在它是我的自定义UIPresentationController之前,但它之后是零.我的猜测是该引用正在发布,但我在Swift中找不到与NS_VALID_UNTIL_END_OF_SCOPE等效的内容.编辑:我已经验证过transitioningDelegate是在演示之前设置的,然后是时候呈现的nil.
我在呈现视图控制器中的代码:
@IBAction func buttonAction(_ sender: UIButton) {
let secondViewController = storyboard!.instantiateViewController(withIdentifier: "NewViewController") as! NewViewController
let presentationController = MyPresentationController(presentedViewController: secondViewController, presenting: self)
presentationController.initialFrame = button.frame
secondViewController.transitioningDelegate = presentationController
// Move map
let pixelsToMove: CGFloat = mapView.frame.height / 4
let region = self.mapView.region
self.mapView.setRegion(region, offsetBy: pixelsToMove, animated: true)
// Delegate to NewViewController
secondViewController.mapView = mapView
mapView.delegate = secondViewController
print(secondViewController.transitioningDelegate)
UIView.animate(withDuration: 0.3, animations: {
let tabBar = self.tabBarController!.tabBar
tabBar.frame.origin.y += tabBar.frame.height
self.present(secondViewController, animated: true, completion: nil)
})
}
Run Code Online (Sandbox Code Playgroud)
我的代码在UIPresentationController中:
override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
presentedViewController.modalPresentationStyle = .custom
}
Run Code Online (Sandbox Code Playgroud)
该transitioningDelegate属性是一个weak var. 请参阅此处的文档。这意味着presentationController设置 时不会增加 的保留计数secondViewController.transitioningDelegate = presentationController。由于您正在presentationController该方法中实例化,并且没有其他任何东西对该对象具有强引用,因此它的保留计数将变为 0,并且一旦从该函数返回控制权(就在 之后print(secondViewController.transitioningDelegate),因为UIView.animate(...)是异步的),它将为零。
presentationController您将需要一些东西来在整个视图控制器的呈现过程中保持强有力的引用。如果某个东西强烈地保留在引用上,那么它的保留计数不会低于 1,除非您专门将该引用设置为nil。一种解决方案是将其保留为当前类的属性或secondViewController.
问题在于 beta 2 中的方法签名发生了UIViewControllerTransitioningDelegate变化,因此它们没有在我的代码中被调用。我不明白为什么,但一切再次完美,无需显式存储对演示控制器的强引用。
| 归档时间: |
|
| 查看次数: |
1173 次 |
| 最近记录: |