【发布时间】:2013-12-12 22:40:51
【问题描述】:
我正在尝试制作一个播放 YouTube 视频的课程,但我遇到了一些问题。
这是我处理 YouTube 视频的课程:
// this is the only property declared in the .h file:
@property (strong, nonatomic) UIView * view
// the rest of this is the .m file:
#import "MyYouTube.h"
@interface MyYouTube()
@property (strong, nonatomic) NSDictionary * contentData;
@property (strong, nonatomic) UIWebView * webView;
@property (nonatomic) int videoOffset;
@end
@implementation MyYouTube
@synthesize view,contentData,webView,videoOffset;
- (MyYouTube*) initIntoView: (UIView*) passedView withContent: (NSDictionary*) contentDict {
NSLog(@"YOUTUBE: begin init");
self=[super init];
videoOffset=0;
view=[[UIView alloc] initWithFrame:passedView.bounds];
[view setBackgroundColor:[UIColor blackColor]];
[view setAutoresizesSubviews:YES];
[view setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
contentData=contentDict;
NSString * compiledUrl=[[NSString alloc] initWithFormat:@"http://_xxx_.com/app/youtube.php?yt=%@",[contentData objectForKey:@"cnloc"]];
NSURL * url=[[NSURL alloc] initWithString:compiledUrl];
NSURLRequest * request=[[NSURLRequest alloc] initWithURL:url];
webView=[[UIWebView alloc] initWithFrame:passedView.bounds];
[webView loadRequest:request];
[webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[[webView scrollView] setScrollEnabled:NO];
[[webView scrollView] setBounces:NO];
[webView setMediaPlaybackRequiresUserAction:NO];
[webView setDelegate:self];
NSLog(@"YOUTUBE: self: %@",self);
NSLog(@"YOUTUBE: delegate: %@",webView.delegate);
[view addSubview:webView];
NSLog(@"YOUTUBE: end init");
return self;
}
-(void)webViewDidFinishLoad:(UIWebView*)myWebView {
NSLog(@"YOUTUBE: send play command");
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"playVideo(%d)", videoOffset]];
}
- (void) dealloc {
NSLog(@"YOUTUBE: dealloc");
}
@end
这里是调用这个类的代码(这个代码位于appDelegate中):
NSLog(@"about to load youtube");
ytObj=[[MyYouTube alloc] initIntoView:mainView withContent:cn];
NSLog(@"loaded youtube");
[mainView addSubview:[ytObj view]];
仅供参考 mainView 和 ytObj 声明如下:
@property (nonatomic,strong) UIView * mainView;
@property (nonatomic,strong) MyYouTube * ytObj;
运行此代码时,应用程序崩溃,我在控制台中得到了这个:
about to load youtube
YOUTUBE: begin init
YOUTUBE: self: <MyYouTube: 0x16503d40>
YOUTUBE: delegate: <MyYouTube: 0x16503d40>
YOUTUBE: end init
YOUTUBE: dealloc
loaded youtube
*** -[MyYouTube respondsToSelector:]: message sent to deallocated instance 0x166f19f0
如果我将 UIWebView 委托设置为 nil,则应用不会崩溃,但正如预期的那样,YouTube 视频不会自动播放。
谁能给我解释一下:
为什么对象在被释放后立即释放 初始化了吗?
为什么
respondsToSelector:消息被发送到 MyYouTube 以外的实例?如何让 YouTube 视频自动播放而不会导致应用崩溃?
非常感谢。
编辑
完全是我的错 - ytObj 是一个强大的属性 - 我忘了提到这一点。我已经添加了代码来反映这一点。
编辑 2
我在dealloc方法上加了一个断点,这就是调用栈:
0 -[MyYouTube dealloc]
1 objc-object::sidetable_release(bool)
2 -[MyAppDelegate playYouTube:]
这里的最后一个条目 (playYouTube:) 是包含上面原始帖子中我的应用程序委托中的代码的方法。所以,还有一个问题:
-
objc-object::sidetable_release(bool)是什么,它有什么作用,它为什么要释放我的 YouTube 对象?
【问题讨论】:
-
只是出于好奇,您能否在
dealloc中放置一个断点并查看它是从哪里调用的(查看调用堆栈)? -
@alex-i 我刚刚为你添加了调用堆栈。
标签: ios objective-c uiwebview youtube