? ? ? ? 這里我要簡單的說一下佣耐,由于react-native不太完善政勃,所以有些涉及到調(diào)用原生這塊對于初學者來說簡直太傷了。今天我給大家分享的是怎樣從react跳轉(zhuǎn)到ios原生界面兼砖,我今天為了這個卡了一天奸远,話不多說,直接上代碼讽挟。純手打懒叛。。耽梅。薛窥。
1.現(xiàn)在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.接下來我們創(chuàng)建個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調(diào)用
最后就不上動圖啦佩番,希望能幫助大家!?