【问题标题】:How to push and present to UIViewController programmatically without segue in iOS Swift 3如何在 iOS Swift 3 中以编程方式推送和呈现给 UIViewController 而无需 segue
【发布时间】:2017-02-17 04:22:07
【问题描述】:

我正在使用此代码在 iOS 中以编程方式推送 SHOWMODALLY > 目标 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];  

【问题讨论】:

    标签: ios swift xcode swift3 uiviewcontroller


    【解决方案1】:

    推送

    喜欢

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

    【讨论】:

    • 你能解释一下or safer这里的部分吗,+1。
    • @vaibhav “或更安全”是指在实例化视图控制器时处理潜在的可选“nil”情况。 "if let" 是在 swift 中处理可选 nil 的经典方式。
    • 如果我在情节提要中没有任何视图控制器,但我正在以编程方式进行操作,那么我应该怎么做才能调用或声明视图控制器的 ID
    • @iOSDeveloper- 如果它在程序上你可以直接调用 VC
    • 为什么需要安全地打开它?它们都导致发生同样的事情 - 如果 navigationController 存在,它会推送视图控制器,否则什么也不会发生。
    【解决方案2】:

    以优雅的方式。

    创建导航协议:

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

    然后您可以将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)
    

    【讨论】:

      【解决方案3】:
      //Create object of view controller 
      let obj = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerIdentifier”) as! ViewControllerName
      
      //Push Controller
      self.navigationController?.pushViewController(obj, animated: true)
      
      //Present Controller
      self.navigationController?.present(obj, animated: true, completion: nil)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多