这里我要简单的说一下,由于react-native不太完善,所以有些涉及到调用原生这块对于初学者来说简直太伤了。今天我给大家分享的是怎样从react跳转到ios原生界面,我今天为了这个卡了一天,话不多说,直接上代码。纯手打。。。。
1.现在app.m让appdelegate成为视图根控制器
#import "AppDelegate.h"
#import "RCTBundleURLProvider.h"
#import "RCTRootView.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *jsCodeLocation;
jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:@"youname"
initialProperties:nil
launchOptions:launchOptions];
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.rootViewController = [UIViewController new];
self.rootViewController.view = rootView;
// self.navi = [[UINavigationController alloc]initWithRootViewController:self.rootViewController];
self.window.rootViewController = self.rootViewController;
[self.window makeKeyAndVisible];
return YES;
}
@end
2.接下来我们创建个NSObject对象
MyVc.h
MyVc.m
这里需要注意的是,必须得在主线程push,其实你不加这个到时候也会报错。
#import "MyVc.h"
#import "RCTBridge.h"
#import "NothingVC.h"
#import "AppDelegate.h"
@implementation MyVc
RCT_EXPORT_MODULE()
- (void)show {
NothingVC *vc = [[NothingVC alloc]init];
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//要在主线程
dispatch_async(dispatch_get_main_queue(), ^{
[delegate.rootViewController presentViewController:vc animated:YES completion:nil];
});
}
RCT_EXPORT_METHOD(showNew) {
[self show];
}
@end
3.配置要push的vc
需要在初始化里写
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
}
4.最后一步要到我们js调用
最后就不上动图啦,希望能帮助大家!☺