2011-01-06 6 views
18

Tôi gặp sự cố lạ với loadHTMLString của UIWebView, nơi nó sẽ chỉ hiển thị chế độ xem trống khi tôi gọi loadHTMLString với chuỗi nội dung HTML của mình. Nó không quan trọng những gì nội dung của htmlstring là, nó chỉ đơn giản là không làm gì cả.Tải UIWebViewHTMLString hiển thị màn hình trống

Điều kỳ lạ là nó được sử dụng để làm việc một vài tuần trước, khi tôi thử nghiệm nó trên giả lập và thiết bị. Mã của tôi là dưới đây:

NSMutableString *sHtmlBuf = [NSMutableString stringWithString:@"<body style=\"background-color: #000000; color: #FFFFFF; font-family: Helvetica; font-size: 10pt; width: 300px; word-wrap: break-word;\">"]; 

if ([m_oCallArray count] > 0 || [m_oPutArray count] > 0) { 
    [sHtmlBuf appendString:sWarrTitle]; 

    if ([m_oCallArray count] > 0) { 
     NSString *formattedCall = [NSString stringWithFormat:@"%@ %@<br />",sCallTitle,[self arrayToString:m_oCallArray]]; 
     [sHtmlBuf appendFormat:@"%@ ",formattedCall]; 
    } 

    if ([m_oPutArray count] > 0) { 
     NSString *formattedPut = [NSString stringWithFormat:@"%@ %@<br />",sPutsTitle,[self arrayToString:m_oPutArray]]; 
     [sHtmlBuf appendFormat:@"%@ ",formattedPut]; 
    } 

} 

if ([m_oBullArray count] > 0 || [m_oBearArray count] > 0) { 
    [sHtmlBuf appendString:sCbbcTitle]; 

    if ([m_oBullArray count] > 0) { 
     NSString *formattedBull = [NSString stringWithFormat:@"%@ %@<br />",sBullTitle,[self arrayToString:m_oBullArray]]; 
     [sHtmlBuf appendFormat:@"%@ ",formattedBull]; 
    } 

    if ([m_oBearArray count] > 0) { 
     NSString *formattedBear = [NSString stringWithFormat:@"%@ %@<br />",sBearTitle,[self arrayToString:m_oBearArray]]; 
     [sHtmlBuf appendFormat:@"%@ ",formattedBear]; 
    } 

} 

if ([m_oOtherArray count] > 0) { 
    NSString *formattedOther = [NSString stringWithFormat:@"%@ %@<br />",sOtherTitle,[self arrayToString:m_oOtherArray]]; 
    [sHtmlBuf appendFormat:@"%@ ",formattedOther]; 
} 

[m_oDataPresentView loadHTMLString:sHtmlBuf baseURL:nil]; 

(Lưu ý: HTML có thể hiển thị trên một trình duyệt web thông thường trước khi vấn đề này, do đó HTML không phải là một vấn đề)

EDIT: nhập mã khởi tạo:

//Create wcbbc panel 
wcbbcPanel = [[WarrantsAndCbbc alloc] initWithFrame:CGRectMake(320, 0, 320, 230)]; 
[m_oMainContentScrollView addSubview:wcbbcPanel]; 
mã khởi

UIView:

- (id)initWithFrame:(CGRect)frame { 

    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code. 
     CGRect oFrame = frame; 
     CGPoint oPositionCoords = CGPointMake(0, 0); 
     oFrame.origin = oPositionCoords; 

     m_oDataPresentView = [[UIWebView alloc] initWithFrame:oFrame]; 
     [m_oDataPresentView loadHTMLString:@"<html><body style=\"background-color: #000000; color: #FFFFFF; font-family: Helvetica; font-size: 10pt; width: 300px; word-wrap: break-word;\"></body></html>" baseURL:nil]; 
     m_oDataPresentView.delegate = self; 

     [self addSubview:m_oDataPresentView]; 
    } 
    return self; 
} 
+0

Bạn có thể hiển thị cho chúng tôi mã nơi bạn khởi tạo 'UIWebView' và thêm mã vào chế độ xem của bạn không? – donkim

Trả lời

40

Sau một chút về công việc thám tử, tôi phát hiện ra rằng trở NO trong hàm đại biểu

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 

sẽ từ chối yêu cầu loadHTMLString. Trả lại YES đã giải quyết được vấn đề của tôi.

+1

Bất kỳ ý tưởng nào tại sao nó không hoạt động trước? I E. Những gì đã thay đổi? –

+0

shouldStartLoadWithRequest hỏi liệu webview có nên tiếp tục tải trang hay không. trả lại KHÔNG dừng trang web tải. – futureelite7

+2

Trong trường hợp của tôi, tôi muốn chặn các liên kết để gửi chúng tới Safari. Để làm điều này: 'if (navigationType == UIWebViewNavigationTypeLinkClicked) trả về NO; trả lại CÓ; '. – Max

0

Từ mã khởi tạo của bạn cho UIWebView, có vẻ như bạn đang thêm wcbbcPanel ở vị trí sẽ tắt màn hình. Hãy thử điều này thay vì:

wcbbcPanel = [[WarrantsAndCbbc alloc] initWithFrame:CGRectMake(0, 0, 320, 230)]; 
6

Tôi chỉ có cùng một vấn đề, và là một n00b, tôi thấy mình ngạc nhiên rằng vấn đề đã được giải quyết khi tôi KHÔNG DO

webview = [[UIWebView alloc]init]; 

nhưng thay vào đó, vì nó đã được dây lên như một lối thoát , tôi chỉ cần gọi

[webView loadHTMLString:mystring baseURL:nil]; 
+0

Điều này đã giúp tôi rất nhiều. Tôi đoán với ARC bạn không cần phải init bạn sở hữu các mục nữa? – jocull