2010-03-02 21 views
6

Quiero generar algo de contenido HTML y ponerlo en un UIWebView. El HTML contiene algunos botones. ¿Es posible definir acciones para estos botones? Quiero llamar a un método (por ejemplo, firstButtonPressed) en mi código objetivo-c cuando alguien presiona este botón en UIWebView.Llamar a un método haciendo clic en un botón en un UIWebView

Gracias por ayudarme.

Respuesta

0

Tome un vistazo al proyecto PhoneGap: www.phonegap.com

Está diseñado para hacer este tipo de cosas y más. En todo caso, podrá hacerse una idea de cómo codificar esto, ya que el proyecto es de código abierto.

4

Solo para dar seguimiento a esto, es posible, y no es difícil conectar eventos HTML/javascript a los métodos de objetivo-c.

Damien Berrigaud ocurrió una good example de cómo hacerlo mediante el uso de enlaces file://

// Map links starting with file:// 
//   ending with #action 
// with the action of the controller if it exists. 
// 
// Open other links in Safari. 
- (BOOL)webView: (UIWebView*)webView shouldStartLoadWithRequest: (NSURLRequest*)request navigationType: (UIWebViewNavigationType)navigationType { 
    NSString *fragment, *scheme; 

    if (navigationType == UIWebViewNavigationTypeLinkClicked) { 
    [webView stopLoading]; 
    fragment = [[request URL] fragment]; 
    scheme = [[request URL] scheme]; 

    if ([scheme isEqualToString: @"file"] && [self respondsToSelector: NSSelectorFromString(fragment)]) { 
     [self performSelector: NSSelectorFromString(fragment)]; 
     return NO; 
    } 

    [[UIApplication sharedApplication] openURL: [request URL]]; 
    } 

    return YES; 
} 
1

este enlace http://davinc.me/post/45670387932/call-ios-method-for-html-button-click me ayuda a resolver el mismo problema.

Para botón

<a href="didTap://button1"><img src="button1.jpg" /></a> 

luego se convierten delegado de

UIWebView

y luego usar

- (BOOL)webView:(UIWebView*)aWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    NSString *absoluteUrl = [[request URL] absoluteString]; 
    if ([absoluteUrl isEqualToString:@"didTap://button1"]) { 
     [self didTapButton1]; 
     return NO; 
    } 
    return YES; 
} 
Cuestiones relacionadas