2010-10-29 4 views
8

He creado un WebView mediante programación. Esto funciona perfecto El código está abajo. Lo que tengo que intentar hacer ahora es inyectar NSStrings en él. Tengo una matriz de 30 cadenas. 15 títulos y 15 textos corporales.Crear un WebView mediante programación y mostrar NSStrings dentro de la vista

¿Es posible que se muestren dentro de WebView? Supongo que necesito cambiarlos a HTML, puedo reformatearlos todos en 1 largo NSString con etiquetas HTML y saltos de línea y etiquetas newling tal vez?

¿Alguien puede ayudarme con algunos punteros/fragmentos de código para ponerme en el camino correcto y mover la dirección correcta, por favor?

- (void) initUIWebView 
{ 
    aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 290)];//init and 
create the UIWebView 

    aWebView.autoresizesSubviews = YES; 
    aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth); 

    [aWebView setDelegate:self]; 
    NSString *urlAddress = @"http://www.google.com"; // test view is working with url to webpage 

    NSURL *url = [NSURL URLWithString:urlAddress]; 

    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 

    [aWebView loadRequest:requestObj]; 

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)]; 
    [[self view] addSubview:aWebView]; 
} 

Gracias -Code

+0

no funciona, ¿puedes publicar un código completo? – NovusMobile

Respuesta

11

En lugar de utilizar el método loadRequest que tendrá que utilizar el método de UIWebView loadHTMLString El siguiente código que podría ayudar en la visualización de la NSString en UIWebView

NSString *html = @"<html><head></head><body>The Meaning of Life<p>...really is <b>42</b>!</p></body></html>"; 
[webView loadHTMLString:html baseURL:nil]; 

Espero que esto resuelva su problema ...

+0

Gracias Atulkumar :) –

Cuestiones relacionadas