首页 > 学院 > 开发设计 > 正文

关于Storyboard拖拽的ViewController返回上一层时有对象被释放

2019-11-06 09:37:25
字体:
来源:转载
供稿:网友

   最近有个需求,设置播放器播放音频时,要求离开当前播放页面也是可以播放的,然后整个项目都是采用storyboard拖拽,播放器设置是采用KVO通知的,当我点击返回时,发现播放器控制对象被释放了,然后导致程序崩溃。于是发现了在storyboard中拖拽的ViewController采用push或者modal弹出时,当点击返回离开当前页面时,strong类型修饰的变量被释放,于是采用代码形式初始化播放页面的ViewController.详见如下代码:

@PRoperty (nonatomic,strong)CoursePlayViewController *courseVC;

- (void)enterPlayViewController

{    if (!self.courseVC) {        self.courseVC = [[CoursePlayViewController alloc] init];    }    [[self getCurrentVC] presentViewController:self.courseVC animated:YES completion:nil];

}

- (UIViewController *)getCurrentVC{    UIViewController *result = nil;        UIWindow * window = [[UIapplication sharedApplication] keyWindow];    if (window.windowLevel != UIWindowLevelNormal)    {        NSArray *windows = [[UIApplication sharedApplication] windows];        for(UIWindow * tmpWin in windows)        {            if (tmpWin.windowLevel == UIWindowLevelNormal)            {                window = tmpWin;                break;            }        }    }        UIView *frontView = [[window subviews] objectAtIndex:0];    id nextResponder = [frontView nextResponder];    if ([nextResponder isKindOfClass:[UIViewController class]])        result = nextResponder;    else        result = window.rootViewController;    return result;}

[self getCurrentVC] 是获取当前页面的视图控制器的方法。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表