如何在iOS Swift 3中以编程方式推送和呈现给UIViewController而不使用segue

我在iOS Objective C中 编程方式使用此代码进行push SHOWMODALLY
现在想知道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]; 

谢谢。

喜欢

 let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc = storyboard.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as NewsDetailsViewController vc.newsObj = newsObj navigationController?.pushViewController(vc, animated: true) 

或者更安全

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

当下

  let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc = self.storyboard?.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as! NewsDetailsViewController vc.newsObj = newsObj present(vc!, animated: true, completion: nil) 

或者更安全

  if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsDetailsVCID") as? NewsDetailsViewController { vc.newsObj = newsObj present(vc, animated: true, completion: nil) } 

以优雅的方式。

创建可导航协议:

 protocol Navigatable { /// Storyboard name where this view controller exists. static var storyboardName: String { get } /// Storyboard Id of this view controller. static var storyboardId: String { get } /// Returns a new instance created from Storyboard identifiers. static func instantiateFromStoryboard() -> Self } 

创建默认的实例化控制器实现:

 /** 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 } } 

扩展UIViewController以推送视图控制器:

 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(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(viewControllerType: T.Type) { self.pushViewControllerOfType(viewControllerType: viewControllerType) { _ in } } } 

然后,您可以将Navigatable协议用于特定的视图控制器。

 class MySuperViewController { override func viewDidLoad() { ... } // ... } extension MySuperViewController: Navigatable { static var storyboardName: String { return "Main" } static var storyboardId: String { 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)