web-dev-qa-db-ja.com

iOSでセグエなしでプログラムでUIViewControllerにプッシュして表示する方法Swift 3

このコードをプッシュに使用していますSHOW and モーダルプログラムでiOSObjective C.
そして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];  

ありがとうございました。

23
reza_khalafi

プッシュ

好きですか

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)
    }
82
Anbu.Karthik

エレガントな方法で。

ナビゲーション可能なプロトコルを作成します。

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を拡張して、View Controllerをプッシュします。

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

その後、特定のView Controllerに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)
11
YMonnier