2016-04-15 4 views
1

Das ist mein Erweiterung:Wie können zwei verschiedene Präsentations-Controller für zwei verschiedene Controller von der Erweiterung von UIViewController zurückgegeben werden?

extension UIViewController: UIViewControllerTransitioningDelegate { 

    func presentAssignBookToClassesViewController(controller: BWAssignBookToClassesViewController) { 

     controller.modalPresentationStyle = .Custom 
     controller.transitioningDelegate = self 
     controller.preferredContentSize = CGSizeMake(500, 575) 

     presentViewController(controller, animated: true, completion: nil) 
    } 

    func presentSettingsStoryboard() { 

     if let settingsController = UIStoryboard(name: "TeacherSettingsStoryboard", bundle: nil).instantiateInitialViewController() { 

      settingsController.modalPresentationStyle = .Custom 
      settingsController.transitioningDelegate = self 
      settingsController.preferredContentSize = CGSizeMake(500, 575) 

      presentViewController(settingsController, animated: true, completion: nil) 
     } 
    } 

    //MARK: - UIViewControllerTransitioningDelegate 

    public func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? { 

     return BWOverlayPresentationController(presentedViewController: presented, presentingViewController: presenting) 
    } 
} 

Innerhalb presentationControllerForPresentedViewController: Ich muss zurückkehren entweder BWOverlayPresentationController oder BWSettingsPresentationController je nachdem, welche Methode aufgerufen wurde. Wie erreiche ich das?

Antwort

1

Sie können einfach unterscheiden sie über restorationIdentifier (Sie diese einfach mit Storyboard einstellen):

//MARK: - UIViewControllerTransitioningDelegate 

public func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? { 

    if presented.restorationIdentifier == BWSettingsRestorationIdentifier { 
     return BWSettingsPresentationController(presentedViewController: presented, presentingViewController: presenting) 
    } else { 
     return BWOverlayPresentationController(presentedViewController: presented, presentingViewController: presenting) 
    } 
} 
1
public func presentationControllerForPresentedViewController(presented: UIViewController?, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? { 

    // You can create some property in presented/presenting viewController. 
    // and check here to return specific viewContoller. 

    if (presented.(somePropertyInViewController)) { 
     return BWOverlayPresentationController(presentedViewController: presented, presentingViewController: presenting) 
    } 
    else { 
     return BWSettingsPresentationController(presentedViewController: presented, presentingViewController: presenting) 
    } 
} 
+0

Aber kann ich dies tun irgendwie global? weil meine Methoden innerhalb der Erweiterung sind, nicht direkt in UIViewController. –

+0

Ich würde vorschlagen, dass Sie einen BaseViewContoller mit zwei viewController-Objekten erstellen können, zB: BWOverlayPresentationController, BWSettingsPresentationController und je nach Bedingung können Sie den spezifischen View-Controller zurückgeben. – Krishna

1

würde ich vorschlagen, Sie eine BaseViewContoller mit zwei Viewcontroller erstellen können Objekte zB: BWOverlayPresentationController, BWSettingsPresentationController und basierend auf Zustand können Sie die den spezifischen View-Controller zurück.

public func presentationControllerForPresentedViewController(presented: UIViewController?, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? { 
let viewController = BaseViewController() 
if (viewController.(somePropertyInViewController)) { 
    return BWOverlayPresentationController(presentedViewController: presented, presentingViewController: presenting) 
} 
else { 
    return BWSettingsPresentationController(presentedViewController: presented, presentingViewController: presenting) 
} 
Verwandte Themen