IOS Universal Jump Interface method (runtime practical Article I)

Source: Internet
Author: User

In the development project, there will be such a perverted need:
    • 推送:根据服务端推送过来的数据规则,跳转到对应的控制器
    • feeds列表:不同类似的cell,可能跳转不同的控制器(嘘!产品经理是这样要求:我也不确定会跳转哪个界面哦,可能是这个又可能是那个,能给我做灵活吗?根据后台返回规则任意跳转?)

思考:wocao!这变态的需求,要拒绝他吗?
switch判断呗,考虑所有跳转的因素?这不得写死我...

switch{    case :        break;    default:        break;}
That's what I realized (runtime is a good thing)

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

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

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{//class name    NSString*class =[NSStringstringwithformat:@"%@", params[@"Class"]];Const Char*classname = [Class cstringusingencoding:nsasciistringencoding];//Return a class from a stringClass Newclass = Objc_getclass (className);if(!newclass) {//Create a classClass superclass = [NSObjectClass]; Newclass = Objc_allocateclasspair (superclass, ClassName,0);//Register the class you createdObjc_registerclasspair (Newclass); }//Create Objects    IDinstance = [[Newclass alloc] init];//Assign a Value property to the object    nsdictionary* Propertys = params[@"Property"]; [Propertys enumeratekeysandobjectsusingblock:^ (IDKeyIDObjBOOL*stop) {//detects if the property exists for this object        if([ SelfCheckisexistpropertywithinstance:instance Verifypropertyname:key]) {//Use KVC to assign values[Instance setvalue:obj Forkey:key]; }    }];//Get navigation controller    Uitabbarcontroller*TABVC = (Uitabbarcontroller*) Self. Window. Rootviewcontroller;Uinavigationcontroller*pushclassstance = (Uinavigationcontroller*) TABVC. Viewcontrollers[TABVC. SelectedIndex];//Jump to the corresponding controller[Pushclassstance pushviewcontroller:instance Animated:YES];}
    • 检测对象是否存在该属性
- (BOOL) Checkisexistpropertywithinstance: (ID) Instance Verifypropertyname: (NSString*) verifypropertyname{unsigned intOutcount, I;//Gets the list of attributes in the objectobjc_property_t * Properties = class_copypropertylist ([instance Class], &outcount); for(i =0; i < Outcount; i++) {objc_property_t property =properties[i];//attribute name to String        NSString*propertyname = [[NSStringAlloc] Initwithcstring:property_getname (property) encoding:nsutf8stringencoding];//Determine if the property exists        if([PropertyName Isequaltostring:verifypropertyname]) {Free (properties);return YES; }} free (properties);return NO;}

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

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

IOS Universal Jump Interface method (runtime practical Article I)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.