我正在使用Objective-C / Swift编写的现有iOS应用程序中集成组件。
作为我的应用的根视图控制器,我使用UINavigationController
。
在我的应用程序的多个视图控制器之一中,我有一个按钮,可以在导航控制器中推送包含以下代码的视图控制器:
@objc class ReactNativeViewController: UIViewController {
override func viewDidLoad() {
let jsCodeLocation = NSURL(string: "http://localhost:8081/index.ios.bundle?platform=ios&dev=true")
let contactsView = RCTRootView(bundleURL: jsCodeLocation, moduleName: "MainComponent", initialProperties: nil, launchOptions: nil)
self.view.addSubview(contactsView)
contactsView.frame = self.view.bounds;
}
}
MainComponent
返回管理多个React-Native组件的Navigator
:
return (
<Navigator
initialRoute={initialRoute}
renderScene={(route, navigator) => {
if (route.component) {
return <route.component navigator={navigator} {...route.passProps} />;
}
}}
navigationBar={
<Navigator.NavigationBar
routeMapper={this.NavigationBarRouteMapper}
style={styles.navBar}
/>
}
/>
);
该工作流程正常。我唯一需要的是在主React-Native组件上点击按钮ReactNativeViewController
时从UINavigationController
弹出Back
的方法。
我试过以下但没有运气:
使用单个方法popLastViewController
创建一个本机模块,弹出从UINavigationController显示的显示的UIViewController:
@implementation RNNavigationControllerBridge
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(popLastViewController) {
UINavigationController *navigationController = (UINavigationController *)[[[[UIApplication sharedApplication] delegate] window] rootViewController];
[navigationController popViewControllerAnimated:NO];
}
@end
按下后退按钮时调用上述方法:
onPress={() => {
if (index === 0) {
NativeViewsManager.popLastViewController();
} else {
navigator.pop();
}
}}
但这不起作用。
有什么建议吗?
答案 0 :(得分:7)
问题解决了!
RCT_EXPORT_METHOD(popLastViewController) {
dispatch_async(dispatch_get_main_queue(), ^{
UINavigationController *navigationController = (UINavigationController *)[[[[UIApplication sharedApplication] delegate] window] rootViewController];
[navigationController popViewControllerAnimated:YES];
});
}