最近的项目多家公司做一个项目 而且都是静态库进行代码合并,appdelegate在人家项目中,本代码实现获取appdelegate中的根导航
[UIApplication sharedApplication].delegate 不用appdelegate的类名强制转换是不能直接点出属性来的,类名未知,所以出此下策
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#import <objc/runtime.h> #import <objc/message.h> +(UINavigationController*)getAppRootNavigationController{ UINavigationController *navController; u_int count; objc_property_t *properties= class_copyPropertyList([[UIApplication sharedApplication].delegate class], &count); for (int i = 0; i < count ; i++) { const char *propertyName = property_getName(properties[i]); NSString *nameSting = [NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding]; const char *propertType =property_getAttributes(properties[i]); NSString *typeSting = [NSString stringWithCString:propertType encoding:NSUTF8StringEncoding]; if ([typeSting rangeOfString:@"Navigation"].location != NSNotFound) { SEL selector = NSSelectorFromString(nameSting); navController= objc_msgSend([UIApplication sharedApplication].delegate, selector); } //free(properties); } return navController; } |
转载请注明:天狐博客 » runtime获取appdelegate中的根导航条属性