dsibiski / react-native-hybrid-app-examples

A collection of examples for using React Native in an existing iOS application
MIT License
228 stars 33 forks source link

Easier-To-Understand Pattern #8

Open idibidiart opened 8 years ago

idibidiart commented 8 years ago

The use of the delegate pattern was a little confusing to me, so I ended up doing this:

//
//  Coordinator.h
//  RNEmbeddedAppExample
//

#import <Foundation/Foundation.h>
#import "RCTBridgeModule.h"
#import "SingleFlowViewController.h"

@interface SomeCoordinator : NSObject <RCTBridgeModule>

@property (nonatomic, assign) SingleFlowViewController *myViewController;

@end

And in ViewController.m

...
 [delegate.bridge.modules[@"SomeCoordinator"] setMyViewController:self];
...

For those of us new to Objective-C I think having the bridge on app delegate and then having to do setDelegate in that line in the implementation causes some confusion, semantically speaking.

Does this make any sense to you?

dsibiski commented 8 years ago

@idibidiart: Yes, this makes sense, and is how I originally did it. However, I avoided this because here the coordinator has to specifically know which ViewController will be using it. You need to explicitly import the .h file, this couples these 2 classes together and can make things harder to change.

The delegate pattern allows any class to be able to use it as long as it implements the proper delegate methods. This keep things cleaner albeit perhaps a bit more confusing to those new to this pattern.

However, I do think that sometimes a coordinator perhaps should be coupled to a specific ViewController. I think it depends on your use case.