iOS 跳转界面方法(runtime)

学习连接:http://www.cocoachina.com/ios/20150824/13104.html


问题:

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

feeds列表:不同的cell,可能跳转不同的控制器


利用runtime动态生成对象、属性、方法这特性,与服务端制定好规则,再用kvc给对象复制


HSFeedsViewController.h:

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

@interface HSFeedsViewController : UIViewController

// 注:根据下面的两个属性,可以从服务器获取对应的频道列表数据

/** 频道ID */

@property (nonatomic, copy) NSString *ID;

/** 频道type */

@property (nonatomic, copy) NSString *type;

@end


AppDelegate.m:

推送过来的消息规则:


NSDictionary *userInfo = @{

@"class": @"HSFeedsViewController",

@"property": @{

@"ID": @"123",

@"type": @"12"

}

};


接受推送消息:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

{

[self push:userInfo];

}


跳转界面


- (void)push:(NSDictionary *)params

{

// 类名

NSString *class =[NSString stringWithFormat:@"%@", params[@"class"]];

const char *className = [class cStringUsingEncoding:NSASCIIStringEncoding];

// 从一个字串返回一个类

Class newClass = objc_getClass(className);

if (!newClass)

{

// 创建一个类

Class superClass = [NSObject class];

newClass = objc_allocateClassPair(superClass, className, 0);

// 注册你创建的这个类

objc_registerClassPair(newClass);

}

// 创建对象

id instance = [[newClass alloc] init];

// 对该对象赋值属性

NSDictionary * propertys = params[@"property"];

[propertys enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

// 检测这个对象是否存在该属性

if ([self checkIsExistPropertyWithInstance:instance verifyPropertyName:key]) {

// 利用kvc赋值

[instance setValue:obj forKey:key];

}

}];

// 获取导航控制器

UITabBarController *tabVC = (UITabBarController *)self.window.rootViewController;

UINavigationController *pushClassStance = (UINavigationController *)tabVC.viewControllers[tabVC.selectedIndex];

// 跳转到对应的控制器

[pushClassStance pushViewController:instance animated:YES];

}


检测对象是否存在该属性


- (BOOL)checkIsExistPropertyWithInstance:(id)instance verifyPropertyName:(NSString *)verifyPropertyName

{

unsigned int outCount, i;

// 获取对象里的属性列表

objc_property_t * properties = class_copyPropertyList([instance

class], &outCount);

for (i = 0; i < outCount; i++) {

objc_property_t property =properties[i];

//  属性名转成字符串

NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];

// 判断该属性是否存在

if ([propertyName isEqualToString:verifyPropertyName]) {

free(properties);

return YES;

}

}

free(properties);

return NO;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 5,865评论 0 9
  • Objective-C语言是一门动态语言,他将很多静态语言在编译和链接时期做的事情放到了运行时来处理。这种动态语言...
    tigger丨阅读 5,342评论 0 8
  • 我们常常会听说 Objective-C 是一门动态语言,那么这个「动态」表现在哪呢?我想最主要的表现就是 Obje...
    Ethan_Struggle阅读 6,602评论 0 7
  • 第一篇文章我们理清了被动词汇量和主动词汇量的区别, 第二篇文章我们理清了词汇量和单词量的区别, 这一篇文章我来回答...
    Wind教口语阅读 8,846评论 0 24
  • 方案一: 1、将微信绑定我的邮箱 2、选中要导出的聊天记录 3、通过邮箱发送到你的指定邮箱 4、打开邮箱即可 优点...
    王菲菲141319阅读 7,220评论 0 0