使用runtime 实现weex 跳转原生页面

一、简述

  最近项目组打算引入weex,并选定了一个页面进行试水。页面很简单,主要是获取数据渲染页面,并可以跳转到指定的页面。跟之前使用RN 相比,weex 确实要简单很多。从下图中我们可以看到,weex 页面需要跳转到原生页面,并且跳转到哪个页面我们可能并不能写死。也就是说只要原生页面之前项目中写过了,那么理论上来说使用weex 可以任意调用。那么问题来了,我原来的页面可能只知道名字,我怎么为那个页面传值呢?比如有个页面orderDetailVC  ,跳转时需要传入orderId,即orderDetailVC.orderId = @"123";

确定了方案之后,剩下唯一的事情就是如何实现给weex 提供的方法。代码如下:

-(void)pushViewController:(NSString *)vcName param:(NSDictionary *)param{
  //获取类
  Class vcClass = NSClassFromString(vcName);
  if (vcClass == nil) {
    return;
  }
  BaseViewController *vc = [[vcClass alloc] init];
  vc.hidesBottomBarWhenPushed = YES;
  //属性数量
  unsigned int count = 0;
  //获取属性列表
  objc_property_t *plist = class_copyPropertyList(vcClass,&count);
  for (int i = 0; i<count; i++) {
    //取出属性
    objc_property_t property = plist[i];
    //取出属性名称
    NSString *propertyName = [NSString stringWithUTF8String:property_getName(property)];
    //以这个属性名称作为key,查看传入的字典里是否有这个属性的value
    if (param[propertyName]) {
      [vc setValue:param[propertyName] forKey:propertyName];
    }
  }
  //释放
  free(plist);
  //获取当前页面控制器
  /*
   获取当前页面控制器是根据响应链获取的。
   */
  UIViewController *currentVC = [Utils getCurrentVC];
  if ([currentVC isKindOfClass:[UINavigationController class]]) {
    [(UINavigationController *)currentVC pushViewController:vc animated:YES];
  }else{
    [currentVC.navigationController pushViewController:vc animated:YES];
  }
}

 经过小规模自测发现是可以实现需求的。但是由于实现时间不长,可能会有不足之处,请谨慎参考。如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

以上是来客网为你收集整理的使用runtime 实现weex 跳转原生页面全部内容,希望文章能够帮你解决使用runtime 实现weex 跳转原生页面所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。