0

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?

1 Answer 1

0

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.