iOS 万能跳转界面方法 (runtime实用篇一)

71 篇文章 0 订阅
66 篇文章 0 订阅

在开发项目中,会有这样变态的需求:

  • 推送:根据服务端推送过来的数据规则,跳转到对应的控制器

  • feeds列表:不同类似的cell,可能跳转不同的控制器(嘘!产品经理是这样要求:我也不确定会跳转哪个界面哦,可能是这个又可能是那个,能给我做灵活吗?根据后台返回规则任意跳转?)

思考:wocao!这变态的需求,要拒绝他吗?

switch判断呗,考虑所有跳转的因素?这不得写死我...

1 switch () {
2     case :
3         break;
4     default:
5         break;
6 }

我是这么个实现的(runtime是个好东西)

利用runtime动态生成对象、属性、方法这特性,我们可以先跟服务端商量好,定义跳转规则,比如要跳转到A控制器,需要传属性id、type,那么服务端返回字典给我,里面有控制器名,两个属性名跟属性值,客户端就可以根据控制器名生成对象,再用kvc给对象赋值,这样就搞定了 ---O(∩_∩)O哈哈哈

比如:根据推送规则跳转对应界面HSFeedsViewController

HSFeedsViewController.h:

  • 进入该界面需要传的属性

1 @interface HSFeedsViewController : UIViewController
2 // 注:根据下面的两个属性,可以从服务器获取对应的频道列表数据
3 /** 频道ID */
4 @property (nonatomic, copy) NSString *ID;
5 /** 频道type */
6 @property (nonatomic, copy) NSString *type;
7 @end

AppDelegate.m:

  • 推送过来的消息规则

1 // 这个规则肯定事先跟服务端沟通好,跳转对应的界面需要对应的参数
2 NSDictionary *userInfo = @{
3                            @"class": @"HSFeedsViewController",
4                            @"property": @{
5                                         @"ID": @"123",
6                                         @"type": @"12"
7                                    }
8                            };
  • 接收推送消息

1 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
2 {
3     [self push:userInfo];
4 }
  • 跳转界面

01 - (void)push:(NSDictionary *)params
02 {
03     // 类名
04     NSString *class =[NSString stringWithFormat:@"%@", params[@"class"]];
05     const char *className = [class cStringUsingEncoding:NSASCIIStringEncoding];
06     // 从一个字串返回一个类
07     Class newClass = objc_getClass(className);
08     if (!newClass)
09     {
10         // 创建一个类
11         Class superClass = [NSObject class];
12         newClass = objc_allocateClassPair(superClass, className, 0);
13         // 注册你创建的这个类
14         objc_registerClassPair(newClass);
15     }
16     // 创建对象
17     id instance = [[newClass alloc] init];
18     // 对该对象赋值属性
19     NSDictionary * propertys = params[@"property"];
20     [propertys enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
21         // 检测这个对象是否存在该属性
22         if ([self checkIsExistPropertyWithInstance:instance verifyPropertyName:key]) {
23             // 利用kvc赋值
24             [instance setValue:obj forKey:key];
25         }
26     }];
27     // 获取导航控制器
28     UITabBarController *tabVC = (UITabBarController *)self.window.rootViewController;
29     UINavigationController *pushClassStance = (UINavigationController *)tabVC.viewControllers[tabVC.selectedIndex];
30     // 跳转到对应的控制器
31     [pushClassStance pushViewController:instance animated:YES];
32 }
  • 检测对象是否存在该属性

01 - (BOOL)checkIsExistPropertyWithInstance:(id)instance verifyPropertyName:(NSString *)verifyPropertyName
02 {
03     unsigned int outCount, i;
04     // 获取对象里的属性列表
05     objc_property_t * properties = class_copyPropertyList([instance
06                                                            class], &outCount);
07     for (i = 0; i < outCount; i++) {
08         objc_property_t property =properties[i];
09         //  属性名转成字符串
10         NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
11         // 判断该属性是否存在
12         if ([propertyName isEqualToString:verifyPropertyName]) {
13             free(properties);
14             return YES;
15         }
16     }
17     free(properties);
18     return NO;
19 }

具体使用和代码: https://github.com/HHuiHao/Universal-Jump-ViewController

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值