这是我的代码
- (void)embedYouTube:(NSString*)url frame:(CGRect)frame {
UIWebView * videoView;
NSString* embedHTML = @"<html><head><style type=\"text/css\">body {background-color: transparent;color: white;}</style></head><body style=\"margin:0\"><embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\"width=\"%0.0f\" height=\"%0.0f\"></embed></body></html>";
NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height];
if(videoView == nil) {
videoView = [[UIWebView alloc] initWithFrame:frame];
[self.view addSubview:videoView];
}
[videoView loadHTMLString:html baseURL:nil];
}
所以在viewDidLoad中我调用
[self embedYouTube:@"http://www.youtube.com/watch?v=2mcjR3TsK4s&feature=g-logo&context=G2e376ceFOAAAAAAAAAA" frame:self.view.bounds];
我在ipad2 ios 5.0中测试它,这是一个错误
- [_ UIAppearance loadHTMLString:baseURL:]:无法识别的选择器发送到实例0x3ec3e63c 2012-01-15 22:50:22.158 test2 [3853:707] * 由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:' - [_ UIAppearance loadHTMLString:baseURL:]:发送到实例的无法识别的选择器 0x3ec3e63c” * 第一次抛出调用堆栈:(0x380cf8bf 0x37c1b1e5 0x380d2acb 0x380d1945 0x3802c680 0x3513 0x3407 0x31bb07ff 0x31baef9d 0x31ba1941 0x31c13541 0x3241 0x31baf7eb 0x31ba93bd 0x31b77921 0x31b773bf 0x31b76d2d 0x37dfddf3 0x380a3553 0x380a34f5 0x380a2343 0x380254dd 0x380253a5 0x31ba8457 0x31ba5743 0x3029 0x2f84)终止被叫 抛出异常(gdb)
感谢您的帮助
答案 0 :(得分:1)
当您定义类似UIWebView * videoView;
的变量时,它假定它引用了某些内存。通常它指的是nil并且一切正常,但在你的情况下它引用另一个对象并且比较videoView == nil
将不起作用。为了避免它,总是声明变量并将其引用到nil
明确。
UIWebView *videoView = nil;