最近有個需求,設置播放器播放音頻時,要求離開當前播放頁面也是可以播放的,然后整個項目都是采用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] 是獲取當前頁面的視圖控制器的方法。
新聞熱點
疑難解答