react-native ios push 本地view

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


.m文件


#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

//
//  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

//
//  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


这样应用程序在进入的时候是js界面,点击Button跳转到我们自定义的TestView界面,点击TestView界面的Button的时候又返回js界面


主要方法为:

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];
  });
}


2.native -- js

- (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];
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值