ios - YouTube WebUIViewDelegate - respondsToSelector : being sent to the wrong instance

标签 ios objective-c youtube

下面是我创建 YouTube 视频的 MyYouTube 类的摘录:

@interface MyYouTube : NSObject <UIWebViewDelegate>
@end

@implementation

- (void) createYouTubeVideoInView: (UIView*) mainView withContent: (NSDictionary*) contentDict {

    // create url request
    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];

    // create the web view
    webView=[[UIWebView alloc] initWithFrame:viewRef.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);

    // add the webview to the main view
    [mainView addSubview:webView];
}

事情是这样的:

如果我将 Delegate 设置为 nil,则 YouTube 视频可以正常播放。

但是

如果我将 Delegate 设置为 self 然后应用程序崩溃并且控制台显示以下内容:

YOUTUBE: self: <MyYouTube: 0x16e05f70>
YOUTUBE: delegate: <MyYouTube: 0x16e05f70>
*** -[MyYouTube respondsToSelector:]: message sent to deallocated instance 0x16e2f420

任何人都可以向我解释为什么实例不同吗?

为什么在与委托(delegate)不同的实例上调用 respondsToSelector:?如何解决这个问题?

非常感谢。

最佳答案

我认为是因为mainView可以释放,导致你的webview在createYouTubeVideoInView方法退出后释放。

确保 mainView 仍然存在。在调用此方法之前将其放置在另一个 View 上。

并确保您对 MyYouTube 对象 (NSObject) 具有强引用。例如,将其设为实例变量。

关于ios - YouTube WebUIViewDelegate - respondsToSelector : being sent to the wrong instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20216176/

相关文章:

objective-c - Dateformatter 更改日期的年份

ios - 带有参数的今日小部件 openurl

java - Mac 上的 SWT 树 - 'Source list'

jquery - 检查 Youtube iFrame 内的内容是否已更改

jquery - Fancybox 包含图像和 YouTube

iOS VIPER : How protocol helps in Unit Testing?

ios - 如何使用转场

iphone - (iphone) 可以为 C++ 类/结构定义 @property 吗?

ios - NSMutableArray 数据按距离排序

c# - youtube 的桌面应用程序(从桌面应用程序登录和更新个人资料)