IOS Universal Jump Interface method

Source: Internet
Author: User

in the development project, there will be such a perverted demand: push: According to the data rule pushed by the server, jump to the corresponding controller feeds list: Different similar cell, may jump different controller (Shh!). The product manager is like this: I'm not sure which interface to jump to Oh, maybe this and maybe that, can you make me flexible? Any jumps based on the background return rule? ) Thinking: wocao! This perverted demand, to reject him? " Switch Judge Bai, consider all the factors of jumping? This must not be written to death me ...123456    Switch () {     Case :         Break; default:         Break;} I realized that (runtime is a good thing) using runtime to dynamically generate objects, properties, methods This feature, we can first negotiate with the server, define the jump rules, such as to jump to a controller, need to pass the property ID, type, then the server returned to me, the controller name , two property names and property values, the client can generate the object according to the controller name, and then use KVC to assign values to the object, so it's done.---o (∩_∩) o hahaha for example: according to push rules jump corresponding interface HSFeedsViewControllerHSFeedsViewController.h: Enter the interface need to pass the property1234567@interface Hsfeedsviewcontroller:uiviewcontroller//Note: According to the following two properties, the corresponding channel list data can be obtained from the server/** Channel ID*/@property (nonatomic, copy) NSString*ID;/** Channel Type*/@property (nonatomic, copy) NSString*type; @endAppDelegate. m: push-over message rule12345678    //This rule must be communicated with the server in advance, the corresponding interface needs to jump to the corresponding parametersNsdictionary *userinfo = @{                           @"class":@"Hsfeedsviewcontroller",                           @" Property": @{                                        @"ID":@"123",                                        @"type":@" A"                                   }                           }; Receive push Messages1234- (void) Application: (UIApplication *) application didreceiveremotenotification: (Nsdictionary *) userinfo{[self push:userinfo];} Jump Interface123456789Ten One A - - the - - - + - + A at - - - - - in - to +- (void) Push: (Nsdictionary *)params{    //class nameNSString *class=[nsstring stringWithFormat:@"%@",params[@"class"]]; Const Char*classname = [classcstringusingencoding:nsasciistringencoding]; //returns 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 that you createdObjc_registerclasspair (Newclass); }    //Creating ObjectsID instance =[[Newclass alloc] init]; //assign a Value property to this objectNsdictionary * Propertys =params[@" Property"]; [Propertys Enumeratekeysandobjectsusingblock:^ (id key, id obj, BOOL *stop) {        //detects if the property exists for this object        if([self checkisexistpropertywithinstance:instance verifypropertyname:key]) {//using KVC to assign values[instance setvalue:obj Forkey:key];    }    }]; //Get navigation ControllerUitabbarcontroller *TABVC = (Uitabbarcontroller *) Self.window.rootViewController; Uinavigationcontroller*pushclassstance = (Uinavigationcontroller *) Tabvc.viewcontrollers[tabvc.selectedindex]; //jump to the corresponding controller[Pushclassstance pushviewcontroller:instance animated:yes];} Detects whether an object exists on this property123456789Ten One A - - the - - - +-(BOOL) Checkisexistpropertywithinstance: (ID) instance verifypropertyname: (NSString *) verifypropertyname{unsignedintOutcount, I; //gets the list of properties in the objectobjc_property_t * Properties =Class_copypropertylist ([instanceclass], &outcount);  for(i =0; i < Outcount; i++) {objc_property_t property=Properties[i]; //property names into stringsNSString *propertyname =[[NSString alloc] Initwithcstring:property_getname (property) encoding:nsutf8stringencoding]; //determine if the property exists        if([PropertyName isequaltostring:verifypropertyname]) {free (properties); returnYES;    }} free (properties); returnNO;}

IOS Universal Jump Interface method

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.