2011-12-15 115 views
1

在這個UIWebView的子類中,我期待設置一個方法來創建嵌入式YouTube視頻。但是,無論我怎麼修改這個代碼(我發現大多數在線的),它總是讓我在該行警告:不兼容的指針類型

self = [[UIWebView alloc] initWithFrame:frame]; 

林不知道這是否是因爲Xcode的4.2或iOS 5或我正在使用自我的事實。有沒有錯嗎?如果是這樣,我該如何解決?

整個方法的代碼:

- (videosView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame; 
{ 
    if (self = [super init]) 
    { 
     // Create webview with requested frame size 
     self = [[UIWebView alloc] initWithFrame:frame]; 

     // HTML to embed YouTube video 
     NSString *youTubeVideoHTML = @"<html><head>\ 
     <body style=\"margin:0\">\ 
     <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \ 
     width=\"%0.0f\" height=\"%0.0f\"></embed>\ 
     </body></html>"; 

     // Populate HTML with the URL and requested frame size 
     NSString *html = [NSString stringWithFormat:youTubeVideoHTML, urlString, frame.size.width, frame.size.height]; 



     // Load the html into the webview 
     [self loadHTMLString:html baseURL:nil]; 
     [self setUserInteractionEnabled:YES]; 

    } 
    return self; 
} 
+0

我希望大家看完我的答案了。它解決了你的警告的性質。 「你正在接受有關不兼容的指針類型的投訴,因爲......」Kevin的答案提供了對你犯的另一個錯誤的信息(在init方法內部分配)並解決了你的問題。 – 2011-12-15 05:22:11

回答

3

你已經擁有的記憶分配(由this指出),所以不要alloc更多:

self = [super initWithFrame:frame]; 

而且,看起來更多一點,你應該不會超過兩倍的init。此舉的語句改成if,而不是普通super init

if (self = [super initWithFrame:frame]) {...} 
+0

關於你答案的第二部分,我是否還會在{...}中保留`self = [super initWithFrame:frame];`並在if語句 – 2011-12-15 04:03:27

0

在你的情況下,自我是指你的類的實例(稱之爲MyWebView:UIWebView的)

在類的外部,我希望你創建喜歡一個實例所以

MyWebView *myInstance = [[MyWebView alloc] initWithStringAsURL:urlString frame:frame]; // release at a later time 

在你的init方法

- (videosView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame; 

您可以通過參考超級

self = [super initWithFrame:frame]; 
if (self != nil) { 
// proceed with custom initialization 
} 

你得到一個關於不兼容的指針類型的投訴,因爲

[[UIWebView alloc] initWithFrame:frame]; 

返回一個UIWebView,不是MyWebView初始化