In a development project, there is a perverse need to:

• Push: Jumps to the corresponding controller according to the data rule pushed by the server

• Lists: Different similar names may jump to different controllers (SHH! The product manager asked: “I am not sure which interface I will jump to. It may be this or that interface. Can you give me flexibility?” Arbitrary jump according to background return rules?

Switch judge, consider all jump factors?

switch() {

case:

break;

default:

break;

}

Or if the else…

It can be solved but it’s too cumbersome if you have 10 possible jumps you have to define 10 types

The old way is as follows:

Method using the runtime dynamically generated objects, attributes, this features, we can discuss it with the server first, define the jump rules, such as to jump to A controller, need to preach attribute id, type, then the server returned the dictionary to me, there is A controller, two attributes and attribute values, the client can be generated according to the controller name objects, Then assign the value to the object with KVC, and you are done

Attributes that need to be passed to enter the interface

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@property (nonatomic, strong) NSString *titleName;

@end

Push or jump message rules

NSDictionary *firstDict = @{

@”class”:@”FirstViewController”,

@”property”:@{

@”titleName”:@”first”

}

};

Jump interface

NSDictionary *info = self.allSelectArray[indexPath.row];

/ / the name of the class

NSString *class = [NSString stringWithFormat:@”%@”,info[@”class”]];

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

// Convert to class by name

Class newClass = NSClassFromString(class);

// Check whether the class exists

if (! newClass) {

// Create a class

Class superClass = [NSObject class];

newClass = objc_allocateClassPair(superClass, className, 0);

// Register this class

objc_registerClassPair(newClass);

}

// Create an object

id instance = [[newClass alloc] init];

// Assign attributes to the object

NSDictionary *propertys = info[@”property”];

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

if ([self checkIsExistPropertuWithInsance:instance verityPropertyName:key]) {

// Use KVC assignment

[instance setValue:obj forKey:key];

}

}];

// Get the navigation controller

[self.navigationController pushViewController:instance animated:YES];

Get navigation controller in push

// Get the 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];

Checks whether the property exists on the object

// Check whether the object has this property

– (BOOL)checkIsExistPropertuWithInsance:(id)instance verityPropertyName:(NSString *)verityPropertyName

{

unsigned int outCount;

// Get the property list of the object

objc_property_t *properties = class_copyPropertyList([instance class], &outCount);

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

objc_property_t property = properties[i];

// Attribute names are converted to characters

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

// Check whether the attribute exists

if ([propertyName isEqualToString:verityPropertyName]) {

free(properties);

return YES;

}

}

free(properties);

return NO;

}

demol

Reference blog:

http://www.cocoachina.com/ios/20150824/13104.html

http://blog.csdn.net/sike2008/article/details/50164961