如何解决视图控制器从本机反应?

时间:2017-07-24 06:27:08

标签: react-native react-native-ios

Integration with exsiting apps doc 展示了如何呈现其内容反应原生的视图控制器。

RCTRootView *rootView =
  [[RCTRootView alloc] initWithBundleURL: jsCodeLocation
                              moduleName: @"RNHighScores"
                       initialProperties:
                         @{
                           @"scores" : @[
                             @{
                               @"name" : @"Alex",
                               @"value": @"42"
                              },
                             @{
                               @"name" : @"Joel",
                               @"value": @"10"
                             }
                           ]
                         }
                           launchOptions: nil];
UIViewController *vc = [[UIViewController alloc] init];
vc.view = rootView;
[self presentViewController:vc animated:YES completion:nil];

它没有告诉你的是如何解散viewcontroller,因为我们处于反应原生(javascript world)而不是objc / swift world ..

如何在viewcontroller上调用native dimiss...函数?

1 个答案:

答案 0 :(得分:3)

检查此回购:tejasd/ios-playground

基本理念:

1.将本机iOS类导入React Native页面,此iOS类做一件事:发布通知

let DismissViewControllerManager = NativeModules.DismissViewControllerManager;

2.在React Native页面中发布通知

onButtonPress: () => {
                        DismissViewControllerManager.goBack();
                    }

3.处理原生iOS页面中的通知并致电dismiss

// add observer in viewDidLoad
NotificationCenter.default.addObserver(self,
                                               selector: #selector(goBack(_:)),
                                               name: NSNotification.Name("dismissViewController"),
                                               object: nil)

// remove observer in deinit (iOS 8 or earlier)
NotificationCenter.default.removeObserver(self)

// handle dismiss notification
func goBack(_ sender: Any?) -> Void {
        self.dismiss(animated: true, completion: nil)
    }