It is the first time I am going for coordinator pattern. Though I have realised it's importance but there is one major concern I have.
I went through this amazing article on this pattern. As a matter of fact I was able to build a demo project on my own using this. There is one point though - use of Xib is proposed. It is not exclusively mentioned that Storyboards can't be used, but going through these lines towards the end of article, makes me think otherwise :
With great power comes great responsibility (and limitations). To use this extension, you need to create a separate storyboard for each UIViewController. The name of the storyboard must match the name of the UIViewController‘s class. This UIViewController must be set as the initial UIViewController for this storyboard.
It isa mentioned that in case of Storyboards, we should create an extension and use that in UIViewController
:
extension MyViewController: StoryboardInstantiable {
}
StoryboardInstantiable :
import UIKit
protocol StoryboardInstantiable: NSObjectProtocol {
associatedtype MyType // 1
static var defaultFileName: String { get } // 2
static func instantiateViewController(_ bundle: Bundle?) -> MyType // 3
}
extension StoryboardInstantiable where Self: UIViewController {
static var defaultFileName: String {
return NSStringFromClass(Self.self).components(separatedBy: ".").last!
}
static func instantiateViewController(_ bundle: Bundle? = nil) -> Self {
let fileName = defaultFileName
let sb = UIStoryboard(name: fileName, bundle: bundle)
return sb.instantiateInitialViewController() as! Self
}
}
Queries :
- As the author mentioned that separate Storyboard would have to be created for each
UIViewController
, how is using Xib a better way in Coordinator pattern ? - Why do we need to create a separate Storyboard for each
UIViewController
? Can't we useUIViewController
's storyboard identifier for that by not linking anyUIViewController
using segues ? That way can adjust the above extension using identifier and easily achieve the same.
I have read that tutorial many times and it uses a Coordinator for each View controller which doesn't make sense to me. I thought that the purpose of a Coordinator was to move the logic of navigation away from the view controllers and into a higher level object that can manage the overall flow.
If you would like to initialise ViewControllers from the main storyboard, use this protocol and extension instead:
The only requirement is that each View controller used by it has this protocol and has a StoryboardID with the same name as the class.
You can use it this way:
Disclaimer: Protocol taken from this article which may also help you
UPDATE: (added reference)
Soroush Khanlou is commonly credited and referenced in other articles and tutorials regarding the coordinator pattern in iOS and Redux. He has an article here (dated 2015, code in objective-c) which you may find to be an interesting read.