J'utilise ce code pour appuyer sur SHOW et MODALEMENT par programmation dans iOS Objective C.
Et maintenant, vous voulez savoir à propos de Swift 3.
NewsDetailsViewController *vc = instantiateViewControllerWithIdentifier:@"NewsDetailsVCID"]; vc.newsObj = newsObj; //--this(SHOW) [self.navigationController pushViewController:vc animated:YES]; //-- or this(MODAL) [self presentViewController:vc animated:YES completion:nil];
Je vous remercie.
Pousser
fais comme
let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc = storyboard.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as NewsDetailsViewController vc.newsObj = newsObj navigationController?.pushViewController(vc, animated: true)
ou plus sûr
if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsDetailsVCID") as? NewsDetailsViewController { viewController.newsObj = newsObj if let navigator = navigationController { navigator.pushViewController(viewController, animated: true) } }
présent
let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc = self.storyboard?.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as! NewsDetailsViewController vc.newsObj = newsObj present(vc!, animated: true, completion: nil)
ou plus sûr
if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsDetailsVCID") as? NewsDetailsViewController { vc.newsObj = newsObj present(vc, animated: true, completion: nil) }
Avec une manière élégante.
Créer un protocole navigable:
protocol Navigatable { /// Storyboard name where this view controller exists. static var storyboardName: Ssortingng { get } /// Storyboard Id of this view controller. static var storyboardId: Ssortingng { get } /// Returns a new instance created from Storyboard identifiers. static func instantiateFromStoryboard() -> Self }
Créez une implémentation de controller d'instanciation par défaut:
/** Extension of Navigatable protocol with default implementations. */ extension Navigatable { static func instantiateFromStoryboard() -> Self { let storyboard = UIStoryboard(name: self.storyboardName, bundle: nil) guard let viewController = storyboard .instantiateViewController(withIdentifier: self.storyboardId) as? Self else { fatalError("Cannot instantiate the controller.") } return viewController } }
Étend le UIViewController
pour pousser un controller de vue:
extension UIViewController { /** Pushes a view controller of the provided type. - Parameter viewControllerType: Type of view controller to push. - Parameter completion: Function to be executed on completion. Contains the view controller that was pushed when successful and nil otherwise. */ func pushViewControllerOfType<T: Navigatable>(viewControllerType: T.Type, completion: (T) -> Void) { let viewController = T.instantiateFromStoryboard() if let vc = viewController as? UIViewController { self.pushViewController(vc, animated: true) } completion(viewController) } } /** Pushes a view controller of the provided type. - Parameter viewControllerType: Type of view controller to push. */ func pushViewControllerOfType<T: Navigatable>(viewControllerType: T.Type) { self.pushViewControllerOfType(viewControllerType: viewControllerType) { _ in } }
Vous pouvez ensuite utiliser le protocole Navigatable
pour un controller de vue spécifique.
class MySuperViewController { override func viewDidLoad() { ... } // ... } extension MySuperViewController: Navigatable { static var storyboardName: Ssortingng { return "Main" } static var storyboardId: Ssortingng { return "MySuperViewControllerId" // From your story board name Main } } // Instantiate your controller let vc = MySuperViewController.instantiateFromStoryboard() // Or // // Push your view controller // testViewController.swift self.pushViewControllerOfType(viewControllerType: MySuperViewController)