2016-10-08 23 views
14

我使用此代碼爲推動SHOW模態編程方式的iOS目標C
現在想知道斯威夫特3.如何在iOS版雨燕推,在場的UIViewController無需編程賽格瑞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]; 

謝謝。

回答

51

不喜歡

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

非常感謝你 –

+1

你能解釋一下比較安全的部分嗎? – vaibhav

+2

@vaibhav「或更安全」是指在實例化視圖控制器時處理潛在的可選'nil'情況。 「如果讓」是在swift中處理可選的零的經典方法。 – Meshach

8

以優雅的方式。

創建一個可導航的協議:

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

然後你可以使用針對特定視圖co的Navigatable協議ntroller。

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)