I'm building a Native Module in Swift for a RN app where I "remove" the RN view and load a Storyboard, everything works fine but I need to send a value from RN to this Module but it is not being saved, I think it's because the controller is loaded again after a call it so the value is lost
This is a simplified version of my code, I call the openMyModule
function on the RN side with the value and it should save the value on a variable, then it calls a function on AppDelegate that will open the Storyboard
@objc (MyModule) class MyModule : UIViewController {
@objc static func requiresMainQueueSetup() -> Bool { return true }
var value = ""
@objc func openMyModule(_ rn_value: String) -> Void {
self.value = rn_value
DispatchQueue.main.async {
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.goToNativeView()
}
}
}
}
This is the function of AppDelegate
- (void) goToNativeView
{
UIViewController *vc = [UIStoryboard storyboardWithName:@"MyModuleScreen" bundle:nil].instantiateInitialViewController;
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
}
I think that when I call goToNativeView
my controller is loaded again, that's why the value is lost
So, what I need is the value to still being available on my controller or save the value on AppDelegate and call it when I need it
There is any way I can achive this?
CodePudding user response:
If anyone else face this problem, here is how I solved:
I created another Swift file to save this value and made the variable static
class AnotherClass {
static var value = ""
}
Then, when I just set the value when I receive it
@objc (MyModule) class MyModule : UIViewController {
@objc static func requiresMainQueueSetup() -> Bool { return true }
@objc func openMyModule(_ rn_value: String) -> Void {
AnotherClass.value = rn_value
DispatchQueue.main.async {
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.goToNativeView()
}
}
}
}
and now it works!