Bonway/BBGestureBack

重置完 rootviewcontroller, BBGestureBaseView 要怎么添加

Opened this issue · 13 comments

重置完 rootviewcontroller, BBGestureBaseView 要怎么添加

重置完rootViewController,是什么意思?

换个了 根控制器?新的根控制器也是这个吗? 有继承这个吗?是 模态出来 一个 新的根控制器?还是?

不是模态 是 UIApplication.shared.keyWindow?.rootViewController = rootViewController;
因为 gestureBaseView 不是要添加到Windows上面吗? 我重新添加无效; 不知道咋解决;

不好意思,我不太清楚您具体是在哪里,怎么样替换rootViewController的,有具体的需求吗?引导图?广告图?或者您能提供下demo,供我参考下,我才能更好的找到问题,因为当初没有这个需求,可能没有涉及到这个问题,望见谅!

ZYHB commented

如下,比如在商品详情发出通知,appdelegate收到通知:
-(void)userLoginStatusNotification:(NSNotification *)noti{
//使用object处理消息
NSString *info = [noti object];
DLog(@"接收 object传递的消息:%@",info);
NSString *token = [kUserDefaults objectForKey:kUserToken];
if (kStringIsEmpty(token)) {
//未登录
UMPLoginViewController *myView = [MAIN_STORY_BOARD(@"UMPLoginStoryboard") instantiateViewControllerWithIdentifier:@"UMPLoginViewController"];
myView.title = @"登录";
UMPBaseNavigationController *navi = [[UMPBaseNavigationController alloc] initWithRootViewController:myView];
self.window.rootViewController = navi;
}else{
//已登录
UMPTabBarViewController *tarBarController = [[UMPTabBarViewController alloc] init];
self.window.rootViewController = tarBarController ;
}
self.gestureBaseView = [[BBGestureBaseView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
[self.window insertSubview:self.gestureBaseView atIndex:0];
self.gestureBaseView.hidden = YES;

// for (UIView *view in self.window.subviews) {
// NSLog(@"再次---------%@",view);
// if ([view isKindOfClass:[BBGestureBaseView class]]) {
// if ([self.window.subviews indexOfObject:view] != 0) {
// BBGestureBaseView *bbview = (BBGestureBaseView *)view;
// //表明该View已被废弃,需要及时删除
// bbview.isDiscarded = YES;
// [bbview removeFromSuperview];
// }
// }
// }
// for (UIView *view in self.window.subviews) {
// NSLog(@"删除之后---------%@",view);
// }
}

我注释的代码如果不打开,那么BBGestureBaseView会重复添加,打开后删除这个View是成功的,没有报错,但是需要在BBGestureBaseView中添加一个属性值,表明该View已经被废弃。

@implementation BBGestureBaseView

  • (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
    self.isDiscarded = NO;
    self.arrayImage = [NSMutableArray array];
    self.backgroundColor = [UIColor blackColor];
    self.imgView = [[UIImageView alloc] initWithFrame:self.bounds];
    self.maskView = [[UIView alloc] initWithFrame:self.bounds];
    self.maskView.backgroundColor = [UIColor colorWithHue:0 saturation:0 brightness:0 alpha:BBMaskingAlpha];
    [self addSubview: self.imgView];
    [self addSubview: self.maskView];
    [[AppDelegate shareAppDelegate].window.rootViewController.view addObserver:self forKeyPath:@"transform" options:NSKeyValueObservingOptionNew context:bbListenTabbarViewMove];
    }
    return self;
    }

  • (void)dealloc{
    if (!self.isDiscarded) {
    [[AppDelegate shareAppDelegate].window.rootViewController.view removeObserver:self forKeyPath:@"transform" context:bbListenTabbarViewMove];
    }

    NSLog(@"%s",FUNCTION);
    }

ZYHB commented

@Bonway 不知道你有没有什么好方法,不知道我的做法是否可行

你好,我也遇到了同样的问题,比如app多tab的情况,切换rootViewController的时候, gestureview重新添加就没什么效果:
//1.

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = [[YTabBarController alloc] init];
    self.gestureBaseView = [[BBGestureBaseView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
    [self.window insertSubview:self.gestureBaseView atIndex:0];
    self.gestureBaseView.hidden = YES;
    [self.window makeKeyAndVisible];
    return YES;
    }
    //2.
  • (void)testClick {
    AppDelegate *appDelegate = [AppDelegate shareAppDelegate];
    Y2TabBarController *cTab = [[Y2TabBarController alloc] init];
    appDelegate.window.rootViewController = cTab;
    self.gestureBaseView = [[BBGestureBaseView alloc] initWithFrame:CGRectMake(0, 0, appDelegate.window.frame.size.width, appDelegate.window.frame.size.height)];
    [appDelegate.window insertSubview:self.gestureBaseView atIndex:0];
    self.gestureBaseView.hidden = YES;
    }
    这种情况怎么办呢?

已收到,已解决,这边正在完善,马上迭代,稍安勿躁

你好,我也遇到了同样的问题,比如app多tab的情况,切换rootViewController的时候, gestureview重新添加就没什么效果:
//1.

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = [[YTabBarController alloc] init];
    self.gestureBaseView = [[BBGestureBaseView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
    [self.window insertSubview:self.gestureBaseView atIndex:0];
    self.gestureBaseView.hidden = YES;
    [self.window makeKeyAndVisible];
    return YES;
    }
    //2.
  • (void)testClick {
    AppDelegate *appDelegate = [AppDelegate shareAppDelegate];
    Y2TabBarController *cTab = [[Y2TabBarController alloc] init];
    appDelegate.window.rootViewController = cTab;
    self.gestureBaseView = [[BBGestureBaseView alloc] initWithFrame:CGRectMake(0, 0, appDelegate.window.frame.size.width, appDelegate.window.frame.size.height)];
    [appDelegate.window insertSubview:self.gestureBaseView atIndex:0];
    self.gestureBaseView.hidden = YES;
    }
    这种情况怎么办呢?

我已经重新上传了demo,望参考

@ZYHB @CivelXu 问题已解决,主要产生的原因是:为了图方便省事,在初始化gestureBaseView的时候,给rootViewController.view addObserver,所以当你们替换掉rootVIewController时,会发现gestureBaseView 产生了问题,主要是KVO没了监听。在新的demo里进行了处理

@ZYHB 大致看了一下解决方案,稍微麻烦了点,其次好像 rootViewController 换掉,没有移除KVO。个人看法,仅供参考~

收到