2011-01-06 30 views
18

Tengo un problema extraño con loadHTMLString de UIWebView, donde solo mostraría una vista en blanco cuando llame a loadHTMLString con mi cadena HTML de contenido. No importa cuál sea el contenido de htmlstring, simplemente no hace nada.UIWebView loadHTMLString muestra la pantalla en blanco

Lo extraño es que solía funcionar hace unas semanas, cuando lo probé en el simulador y el dispositivo. Mi código es el siguiente:

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]; 

(Nota: el código HTML puede hacer en un navegador web normal antes de este problema, por lo que el HTML no es un problema)

EDIT: Añadido código de inicialización:

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

UIView código de inicialización:

- (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

¿Podría mostrarnos el código donde crea una instancia de 'UIWebView' y agregarlo a su vista? – donkim

Respuesta

40

Después de un poco de trabajo de investigación, descubrí que la devolución de NO en la función de delegado

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

rechazará las solicitudes loadHTMLString. De vuelta SÍ resolvió mi problema.

+1

¿Alguna idea de por qué no funcionó de antemano? es decir. ¿Qué cambió? –

+0

shouldStartLoadWithRequest pregunta si la vista web debe continuar cargando la página. devolver NO detiene la carga de la página web. – futureelite7

+2

En mi caso, quería interceptar enlaces para enviarlos a Safari. Para hacer esto: 'if (navigationType == UIWebViewNavigationTypeLinkClicked) return NO; si no, devuelve SÍ; '. – Max

0

Desde su código de inicialización para el UIWebView, parece que está agregando wcbbcPanel en una posición que estaría fuera de la pantalla. Prueba este lugar:

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

yo sólo tenía el mismo problema, y ​​de ser un n00b, me encontré sorprendido de que el problema se resolvió cuando i no hice

webview = [[UIWebView alloc]init]; 

pero en cambio, puesto que ya se cablea como una salida , solo tenía que llamar a

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

Esto me ayudó mucho. Supongo que con ARC ya no tienes que iniciar tus propios artículos. – jocull

Cuestiones relacionadas