简体   繁体   English

ios - 如何在iOS Swift 3中以编程方式推送和呈现给UIViewController而无需segue

[英]How to push and present to UIViewController programmatically without segue in iOS Swift 3

I am using this code for push SHOW and MODALLY programmatically in iOS Objective C .我正在使用此代码在iOS Objective C 中编程方式推送SHOWMODALLY
And now want to know about Swift 3.现在想了解 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];  

Push

do like 喜欢

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

or safer 或者更安全

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

present 当下

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

or safer 或者更安全

   if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsDetailsVCID") as? NewsDetailsViewController
     {

     vc.newsObj = newsObj
    present(vc, animated: true, completion: nil)
    }

With an elegant way. 以优雅的方式。

Create an Navigatable protocol: 创建可导航协议:

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
}

Create a default instantiate controller implementation: 创建默认的实例化控制器实现:

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

Extends the UIViewController to push a view controller: 扩展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 }
    }
}

Then you can use the Navigatable protocol for a specific 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)
//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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式从 iOS Swift 3 - How to push and present to UIViewController programmatically From a XIB in iOS Swift 3 iOS:以编程方式呈现Segue,而不会丢失导航 - iOS: Present segue programmatically without loosing the navigation 如何在 iPhone 上以编程方式在 Swift 中将 UIViewController 显示为弹出框 - How to present a UIViewController as a popover in Swift programmatically on iPhone 如何在 Swift 4 中显示没有 UIViewController 的新屏幕? - How to present a new screen without a UIViewController in Swift 4? IOS - 如何使用swift以编程方式进行segue - IOS - How to segue programmatically using swift 如何以编程方式从一个UIViewController切换到另一个,而无需模态或Push? - How to programmatic segue from one UIViewController to another without Modal or Push? 在另一个UIViewController中更新UITableView而无需在Swift中进行设置 - Updating UITableView in another UIViewController without a segue in Swift 如何以编程方式从 UITableViewController 顺利切换到 UIViewController - How to segue smoothly from UITableViewController to UIViewController programmatically 如何以编程方式从UINavigationController(storyboard)到UIViewController - How to segue from UINavigationController (storyboard) to UIViewController programmatically 如何在PageViewController的UIViewController中以编程方式使用序列 - How to use a segue, programmatically, in a UIViewController of a PageViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM