? ? ? ? 這里我要簡(jiǎn)單的說(shuō)一下,由于react-native不太完善,所以有些涉及到調(diào)用原生這塊對(duì)于初學(xué)者來(lái)說(shuō)簡(jiǎn)直太傷了。今天我給大家分享的是怎樣從react跳轉(zhuǎn)到ios原生界面,我今天為了這個(gè)卡了一天,話不多說(shuō),直接上代碼。純手打。。。。
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.接下來(lái)我們創(chuàng)建個(gè)NSObject對(duì)象
MyVc.h
MyVc.m
這里需要注意的是,必須得在主線程push,其實(shí)你不加這個(gè)到時(shí)候也會(huì)報(bào)錯(cuò)。
#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
需要在初始化里寫(xiě)
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
}
4.最后一步要到我們js調(diào)用
最后就不上動(dòng)圖啦,希望能幫助大家!?