IOS ViewController 切换笔记参考:http://onevcat.com/2013/10/vc-transition-in-ios7/
react-native JS端定义一个简单的button就可以了,点击调用IOS原生如下:
.h文件
#import <UIKit/UIKit.h> #import "Base/RCTBridgeModule.h" #import "RCTUtils.h" #import "AppDelegate.h" @interface XXX : NSObject <RCTBridgeModule> @end
#import "XXX.h"
#import "Base/RCTLog.h" #import "TestViewController.h" @implementation XXX RCT_EXPORT_MODULE() #pragma mark "API" RCT_EXPORT_METHOD(login:(RCTResponseSenderBlock)callback) { [self presentRegistrationViewController]; } - (void)presentRegistrationViewController { UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow]; UIViewController *mainViewController = keyWindow.rootViewController; TestViewController *bceRegistrationViewController = [TestViewController new]; dispatch_async(dispatch_get_main_queue(), ^{ [mainViewController presentViewController:bceRegistrationViewController animated:YES completion:nil]; }); } @end
// // TestViewController.h // MagicWindowTest // // Created by wangyanan on 15/11/17. // Copyright © 2015年 Facebook. All rights reserved. // #import <UIKit/UIKit.h> @interface TestViewController : UIViewController @end
// // TestViewController.m // MagicWindowTest // // Created by wangyanan on 15/11/17. // Copyright © 2015年 Facebook. All rights reserved. // #import "TestViewController.h" #import "RCTRootView.h" @interface TestViewController () @end @implementation TestViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. CGRect viewSize=self.view.bounds; UIButton*loginButton=[[UIButton alloc]initWithFrame:CGRectMake(viewSize.size.width/2-50, viewSize.size.height/2, 100, 50)]; [loginButton setTitle:@"登录" forState:UIControlStateNormal]; loginButton.backgroundColor=[UIColor blueColor]; [loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:loginButton]; } -(void) login { NSLog(@"login"); [self presentRegistrationViewController]; } - (void)presentRegistrationViewController { NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localHost:8081/index.ios.bundle?platform=ios&dev=true"]; RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName:@"MagicWindowTest" initialProperties:nil launchOptions:nil]; UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow]; UIViewController *mainViewController = keyWindow.rootViewController; mainViewController.view = rootView; [self.navigationController pushViewController:mainViewController animated:true]; [self dismissViewControllerAnimated:YES completion:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
主要方法为:
1.js -- native
- (void)presentRegistrationViewController { UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow]; UIViewController *mainViewController = keyWindow.rootViewController; TestViewController *bceRegistrationViewController = [TestViewController new]; dispatch_async(dispatch_get_main_queue(), ^{ [mainViewController presentViewController:bceRegistrationViewController animated:YES completion:nil]; }); }
- (void)presentRegistrationViewController { NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localHost:8081/index.ios.bundle?platform=ios&dev=true"]; RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName:@"MagicWindowTest" initialProperties:nil launchOptions:nil]; UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow]; UIViewController *mainViewController = keyWindow.rootViewController; mainViewController.view = rootView; [self.navigationController pushViewController:mainViewController animated:true]; [self dismissViewControllerAnimated:YES completion:nil]; }