简体   繁体   中英

How can I create an instance of a custom model with multiple viewcontrollers in Xcode with Swift 4?

I'm new to Swift and I'm sure this question is pretty basic and has been asked and answered before.

I am not using storyBoard . My main viewController is created from AppDelegate via code.

I have:

  • a custom class defined in a model.swift file
  • a main viewController (from AppDelegate ) that I am using as a container
  • 3 additional viewcontrollers as subviews of the main (not each other)
  • all 3 subviews are displayed simultaneously each covering 1/3 of the screen (no segues)
  • each viewcontroller is in a separate .swift file

I want to create an instance of my custom class in the main viewController and have all 3 of the subviews be able to reference that instance.

Each of the subview view controllers need to be able to get/set instance variables and the other subviews need to be made aware of those changes.

I think I will need to use notifications to communicate the changes to the multiple subviews - but I haven't even begun to try and figure that out yet.

If this has been asked and answered before - could someone please either provide a link - or provide me with the right search terms so that I'm able to find the answer? The only found answers I've found that come close are to use segues to pass the data back and forth.

You can use delegate pattern. Below code is assuming that you are using MVVM pattern. (It is very similar for VIPER / ReSwift patterns also)

protocol DataChangedDelegate {
  func refreshData()
}

// ViewModel for FirstViewController
class FirstViewModel {
   var delegate: DataChangedDelegate?

   var data: Any {
     didSet {
       delegate?.refreshData()
     }
  }
  //rest of the things
}

//similarly other two view models will have a delegate and on data change will call the refresh method

And your view controllers should adopt this protocol

class FirstViewController: UIViewController, DataChangedDelegate {
  //view controller code

  //delegate code
  func refreshDate() {
    //tableView.reloadDate()
    //collectionView.reloadDate()
    //lableView.text = viewModel.data()
  }
}

And where ever you create a viewControllers and add as subView, you have to set the delegate of viewModel.

let firstViewController: FirstViewController = createFirstViewController()
let firstViewModel = FirstViewModel()
firstViewModel.delegate = firstViewController
firstViewController.viewModel = firstViewModel
mainViewController.addSubView(firstViewController.view)

Similarly for all other view controllers.

Here's how I would do it:

  1. Create a singleton class.
  2. Configure the singleton's properties in the the main ViewController.
  3. Use didSet to post a Notification.
  4. Add a listener for that Notification in your additional ViewControllers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM