前言
对于IM页面开发中,经常会碰到跳转到一个UINavigationController的子控制器栈堆中已经存在的ViewController的情况。
出现步骤:
eg1- ab ab 循环
- 进入IM聊天,
- 点击头像,
- 点击聊天,
eg2- abc abc循环
- 进入IM聊天,
- 点击右上角进入设置,
- 点击头像或者搜索聊天记录,
- 点击聊天
这种情况其实微信中也处理的不是特别好:群聊中点击某个人,发起聊天后,会退回root,然后再push到与这个人的会话中。
一般我们会认为相同的对象才会没有存在的价值,而和某人的P2P聊天与之前的Team聊天是完全不同的内容。假如是我还想与群内别人交涉,就要重新找到这个群再进行额外频繁操作了。
基于这个设想,我认为减少出现相同控制器的过程,应该对控制器的唯一属性也进行一个确认相同操作。
实现思路
- 跳转前对需要去冗的ViewController设置待判断的key-values,
- 对UINavigationController的pushViewController:animated:进行方法替换:跳转前发现栈堆中有相同Class时,对两个ViewController的key-values对应确认。
- 如果key-values相同,则用方法popToViewController:animated:跳转回到已经存在的ViewController,否则就进行正常push操作。
实现代码
1. UIViewController 分类
添加操作方法和对比属性,不设置的界面则不受影响。
@interface UIViewController (SameControllerInStack)
@property (nonatomic, strong) NSDictionary * sameConfirmPropertys;
- (void)gobackIfAlreadyInStackConfirmBy:(NSDictionary *)propertys;
@end
const void *const kSameConfirmPropertys = &kSameConfirmPropertys;
@implementation UIViewController (SameControllerInStack)
- (NSDictionary *)sameConfirmPropertys {
return objc_getAssociatedObject(self, &kSameConfirmPropertys);
}
- (void)setSameConfirmPropertys:(NSDictionary *)sameConfirmPropertys {
objc_setAssociatedObject(self, kSameConfirmPropertys, sameConfirmPropertys, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)gobackIfAlreadyInStackConfirmBy:(NSDictionary *)propertys {
self.sameConfirmPropertys = propertys;
}
@end
2. UINavigationController 分类
push方法替换,在方法中进行同属性控制器的判断
@interface UINavigationController (SameControllerInStack)
@end
@implementation UINavigationController (SameControllerInStack)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
@autoreleasepool {
[self swizzleMethod:@selector(pushViewController:animated:) swizzledSelector:@selector(swizzle_pushViewController:animated:)];
}
});
}
- (void)swizzle_pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
NSDictionary * confirmPropertys = viewController.sameConfirmPropertys;
BOOL isSame;
if (confirmPropertys.count) {
for (UIViewController * vc in self.viewControllers) {
if ([vc isKindOfClass:viewController.class]) {
BOOL isSame = [self samePropertyOfVC1:vc VC2:viewController propertys:confirmPropertys];
if (isSame) {
[self popToViewController:vc animated:animated];
return;
}
}
}
}
[self swizzle_pushViewController:viewController animated:animated];
}
- (BOOL)samePropertyOfVC1:(id)vc1 VC2:(id)vc2 propertys:(NSDictionary *)propertys {
for (NSString * property in propertys) {
id object1 = [vc1 valueForKey:property];
id object2 = [vc2 valueForKey:property];
if (![object1 isEqual:object2]) {
return NO;
}
}
return YES;
}
3. NSObject 分类
进行方法替换需要的简化方法
@interface NSObject (SwizzleMethod)
- (void)swizzleMethod:(SEL)originalSelector swizzledSelector:(SEL)swizzledSelector;
@end
@implementation NSObject (SwizzleMethod)
- (void)swizzleMethod:(SEL)originalSelector swizzledSelector:(SEL)swizzledSelector {
Class class = [self class];
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod = class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
@end
实例参考
添加后在个人名片页面(个人信息),搜索聊天记录等可能出现重复界面的操作时,
根据业务需要,加上一句gobackIfAlreadyInStackConfirmBy:就可以了。
IM_Session_MessageVC *vc = [[IM_Session_MessageVC alloc] initWithSessionId:self.userId sessionType:0];
[vc gobackIfAlreadyInStackConfirmBy:@{@"sessionId" : self.userId,
@"sessionType" : @(0), // P2P
}];
[self.navigationController pushViewController:vc animated:YES];
PS
本文只是个人见解,如果发现有问题或者有更好方案的话,请不吝赐教,共同学习进步。
qq:12087014
email: [email protected]