Losing link to previous presentation before presenting prefetch

I had a working user UIPresentationController before Xcode beta 2 and iOS 10 beta 2. I did not change any code, but the presentation now provides a standard modal presentation.

There is a note in the Apple sample code for the UIPresentationController:

For presentations that will use a custom view controller, there may also be transitioningDelegate for this view controller. This avoids introducing another object or embedding into a view source.

transitioningDelegate does not contain a link to its target. To prevent presentationController from being released before calling -presentViewController: animated: completion: the NS_VALID_UNTIL_END_OF_SCOPE attribute is added to the declaration.

I tested transitioningDelegate on the presented view controller before and after the presentation. Before it will be my custom UIPresentationController, but after it is zero. I assume the link is being released, but I cannot find the equivalent of NS_VALID_UNTIL_END_OF_SCOPE in Swift. EDIT: I checked that transitioningDelegate is configured only before the presentation, and then is zero when it is time to submit.

My code in the view controller:

@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) }) } 

And my code in the UIPresentationController:

 override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) { super.init(presentedViewController: presentedViewController, presenting: presentingViewController) presentedViewController.modalPresentationStyle = .custom } 
+7
ios uiviewcontroller swift uipresentationcontroller
source share
2 answers

The problem was in beta 2, when the method signatures in the UIViewControllerTransitioningDelegate were changed, and therefore they were not called in my code. I don’t understand why, but once again everything works perfectly, without explicitly storing a strong link to the view controller.

+2
source share

The transitioningDelegate property is weak var . See here. This means that the hold value for presentationController does not increase when setting secondViewController.transitioningDelegate = presentationController . Since you instantiate the presentationController in this method, and nothing else has a strong reference to this object, it retains a count value of 0, and it will be zero as soon as the control returns from this function (immediately after print(secondViewController.transitioningDelegate) , since UIView.animate(...) is asynchronous).

You will need something to maintain a strong reference to the presentationController during the presentation controller's presentation. With something tightly attached to the link, it keeps the counter will not be lower than 1 until you specifically set this link to nil . One solution would be to save it as a property of the current class or property of secondViewController .

+2
source share

All Articles