7

私は、Xcode beta 2とiOS 10 beta 2より前に働くカスタムUIPresentationControllerを持っていました。私はコードを変更していませんが、プレゼンテーションは標準モーダルプレゼンテーションで表示されています。そのプレゼンテーションコントローラも transitioningDelegateするためのカスタムプレゼンテーションコントローラを使用するプレゼンテーション用プレゼンテーションプレゼンテーションの前にtransitioningDelegateへの参照を失う

は、 ことが可能です:

氏は述べていますUIPresentationControllerのためのAppleのサンプルコードでの注意事項があります。これにより、別のオブジェクトまたは が導入され、ソースには ビューコントローラが実装されなくなります。アニメーション:終了: NS_VALID_UNTIL_END_OF_SCOPE属性が 宣言に追加され

transitioningDelegateは 解放-presentViewController呼び出す前であることからpresentationControllerを防ぐobject.Toその宛先に強い参照を保持しません。

プレゼンテーションの前後に、提示されたビューコントローラでtransitioningDelegateを確認しました。私のカスタムUIPresentationControllerの前ですが、後は​​それがnilです。私の推測では、リファレンスはリリースされているが、SwiftではNS_VALID_UNTIL_END_OF_SCOPEと同等のものは見つからない。 EDIT:私は、transitioningDelegateがプレゼンテーションの直前まで設定されていることを確認しました。

提示ビューコントローラでの私のコード:

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

そしてUIPresentationControllerでの私のコード:

override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) { 
    super.init(presentedViewController: presentedViewController, presenting: presentingViewController) 
    presentedViewController.modalPresentationStyle = .custom 
} 
+0

メソッド外にある前にpresentationControllerを宣言してみてください。 –

+0

試しましたが、それでも同じ問題があります。 – jjatie

+0

これを試してくださいself.presentedViewController.transitioningDelegate –

答えて

2

問題はベータ2で、UIViewControllerTransitioningDelegateのメソッドシグネチャが変更されたため、コード内で呼び出されていませんでした。私は理由を理解できませんが、プレゼンテーションコントローラへの強力な参照を明示的に保存しなくても、すべてが完璧に機能します。

+0

それは私の問題でした。デリゲートプロトコルにはオプション機能がすべて含まれているため、誤ったシグネチャを持つ関数は暗黙のうちに無視されます。詳細はhttp://stackoverflow.com/a/39513247/959896をご覧ください。 –

2

transitioningDelegateプロパティがweak varです。 docs hereを参照してください。つまり、secondViewController.transitioningDelegate = presentationControllerを設定すると、presentationControllerの保持カウントは増加しません。そのメソッドでpresentationControllerをインスタンス化しているため、そのオブジェクトへの強い参照がない場合、保持カウントは0になり、その関数から制御が返されるとすぐにゼロになります(UIView.animate(...)は非同期であるためprint(secondViewController.transitioningDelegate)の直後) 。

ビューコントローラのプレゼンテーションを通して、presentationControllerを強く参照するために何か必要があります。何かが強く参照を保持している場合、その参照を具体的にnilに設定するまで、保持カウントは1未満になりません。 1つの解決策は、それを現在のクラスのプロパティまたはsecondViewControllerのプロパティとして保持することです。

+0

今日これを試してみます。なぜiOS 10 beta 1と9.3で動作したのでしょうか?xはそれに強い参照を作成せずに? – jjatie

+0

残念ながら、わかりません。私はこれも以前にはうまくいかなかっただろうと推測しました。 – keithbhunter

+0

プレゼンテーションの際にプレゼンテーションとプレゼンテーションコントローラに強い参照を付けて保存しようとしましたが、プレゼンテーションの際にtransitioningDelegateはまだゼロになっています – jjatie