简体   繁体   English

如何在 MVVM 中快速从一个屏幕导航到另一个屏幕

[英]How to Navigate from one screen to another in swift Working in MVVM

I am doing this with out working in MVVM as follows, but what will be the best approach for navigation in MVVM, if possible kindly consider both cases -navigation with or with out data, any clue will be appreciated: Thanks我这样做没有在 MVVM 中工作,如下所示,但是在 MVVM 中导航的最佳方法是什么,如果可能,请考虑这两种情况 - 有或没有数据的导航,任何线索将不胜感激:谢谢

let st = UIStoryboard.init(name: "Register", bundle: nil)
    let register = st.instantiateViewController(withIdentifier: "RegisterVC" ) as! RegisterVC
register.data  =  self.data
self.navigationController?.pushViewController(register, animated: true)`

This is how I have been navigating between view controllers for sometime now, and have found it to be quite effective.这就是我一段时间以来一直在视图控制器之间导航的方式,并且发现它非常有效。 My ViewControllers will generally each have a ViewModel class, which is responsible for logic and/or data loading or saving.我的 ViewControllers 通常每个都有一个 ViewModel 类,它负责逻辑和/或数据加载或保存。 I instantiate the view model properties like this in each view controller:我在每个视图控制器中实例化这样的视图模型属性:

class ExampleViewController: UIViewController {
  lazy var viewModel = ExampleViewModel()

  override func viewDidLoad() {
    super.viewDidLoad()

    // Load data to the controller via the view model here
    viewModel.loadSomeData()
  }

  // This could be linked to a button from the UI builder
  @IBAction func nextPressed(_ sender: Any) {
    // Possibly use the view model to persist data here
    viewModel.saveSomeData()

    // Then navigate to the next controller
    self.navigationController?.pushViewController(NextViewController(), animated: true)
  }
}

In the above example, NextViewController would then have its own view model which would handle it's data loading and persistence.在上面的示例中, NextViewController 将拥有自己的视图模型,该模型将处理它的数据加载和持久性。 How you return the data from the view model to the controller is up to you.如何将数据从视图模型返回到控制器取决于您。 If its simple data, you can just return it from the functions, otherwise for more complex or a-synchronous functions you could use a delegate or a closure.如果是简单的数据,您可以从函数中返回它,否则对于更复杂或异步的函数,您可以使用委托或闭包。 Keeping the data out of the controller can stop the class from getting too bloated, and reduces the dependancy of controllers on each other.将数据保留在控制器之外可以防止类变得过于臃肿,并减少控制器之间的依赖性。

So you would generally have some kind of data layer (possible more than one) under the view model.因此,您通常会在视图模型下拥有某种数据层(可能不止一个)。 This could be an API that the view model calls, or the app could have a local database.这可以是视图模型调用的 API,或者应用程序可以具有本地数据库。 And the view model keeps that separate from the controller and views.并且视图模型将其与控制器和视图分开。

On a slightly unrelated side note, I also prefer to have my controllers as swift and .xib files, rather than using a storyboard, when possible.在一个稍微不相关的旁注中,如果可能的话,我也更喜欢将我的控制器作为 swift 和 .xib 文件,而不是使用故事板。 I find big story boards tend to get very slow and eventually become tedious to make changes to.我发现大的故事板往往变得非常缓慢,最终变得乏味,无法进行更改。 You can also then initialise the controllers like i did in the example, and don't need to create a storyboard object first.然后,您还可以像我在示例中所做的那样初始化控制器,而无需先创建故事板对象。 But this is just personal preference.但这只是个人喜好。

extension UIViewController { class func loadNib() -> Self { return loadNib(self) } }扩展 UIViewController { class func loadNib() -> Self { return loadNib(self) } }

let travelVC = ItenararyConfirmationViewController.loadNib() navigationController?.pushViewController(travelVC, animated: true) let travelVC = ItenararyConfirmationViewController.loadNib() navigationController?.pushViewController(travelVC, animation: true)

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

相关问题 React Native:如何在 Detox 测试中从一个屏幕导航到另一个屏幕 - React Native: How to navigate from one screen to another in Detox tests 如何通过单击按钮在 Swift UI 中从一个视图导航到另一个视图 - How to navigate from one view to another in Swift UI on a click of button 如何在 Swift 中以编程方式从一个控制器导航到另一个视图控制器? - How to navigate from one controller to another View Controller programmatically in Swift? 如何使用 Swift 从一个视图控制器导航到另一个视图控制器 - How to Navigate from one View Controller to another using Swift 如何从一个故事板导航到另一个 TabBarController? - How to navigate from one storyboard to another TabBarController? 如何快速从一个屏幕更改另一个屏幕的颜色? - How can i change color of one screen from another screen in swift? 从幻灯片菜单导航到另一个 viewController? 迅速 - Navigate to another viewController from slide menu? SWIFT 如何在Ios中从一个项目情节提要导航到另一个项目情节提要 - How to navigate from one project storyboard to another project storyboard in Ios 如何从一个视图控制器导航到另一视图控制器? - How to navigate from one view controller to another view controller? 如何从一个视图导航到另一个视图而无需导航? 在iPhone中 - how to navigate from one view to another without navigation? in iphone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM