无法在UIWebView中执行javascript函数?

时间:2014-01-30 03:26:10

标签: javascript ios objective-c uiwebview

我有两个视图控制器,viewcontroller 1和视图控制器2.viewController 1包含一个webview。当我单击webview中的链接时,我将视图控制器2推到视图控制器1的顶部。在视图控制器2中扫描条形码后,我将扫描数据作为函数传递给viewcontroller1。然后我从屏幕顶部弹出viewController 2。我无法访问viewcontroller 1中的webview属性并将条形码显示为javascript警报。感谢你的帮助。

这是viewController 1

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>




@interface ADMSViewController : UIViewController <UIWebViewDelegate>
{




}
@property (nonatomic,retain) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
@property (nonatomic) BOOL restoringState;


- (IBAction)backButtonPressed:(id)sender;
-(void)pasteVinInTextBox : (NSString *)vin;

@end

这是相应的.m文件

#import "ADMSViewController.h"

@implementation ADMSViewController


- (void)viewDidLoad {
    [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    [_webView setDelegate:self];
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com/"]]];
     }

//- (void)didReceiveMemoryWarning //{ //    [super didReceiveMemoryWarning]; //    // Dispose of any resources that can be recreated. //}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated]; }

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated]; }


-(void)webViewDidStartLoad:(UIWebView *)webView {
    [_activityIndicator startAnimating]; }

- (void)viewDidUnload {
    [super viewDidUnload]; }

-(void)webViewDidFinishLoad:(UIWebView *)webView {
    [_activityIndicator stopAnimating]; }

- (IBAction)backButtonPressed:(id)sender {
    [_webView goBack]; }

-(void)pasteVinInTextBox:(NSString *)vin {
    NSLog(@"%@",vin);
    _webView = [[UIWebView alloc]init];
    [_webView stringByEvaluatingJavaScriptFromString:@"alert('Trigger the JS!');"]; }



@end

这是barcodeScanner.m文件

#import "ADMSBarcodeScanner.h"


@implementation ADMSBarcodeScanner


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

    }

    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // ADD: present a barcode reader that scans from the camera feed
    ZBarReaderViewController *reader = [[ZBarReaderViewController alloc] init];
    reader.readerDelegate = self;
    reader.supportedOrientationsMask = ZBarOrientationMask(UIInterfaceOrientationLandscapeRight);

    ZBarImageScanner *scanner = reader.scanner;
    // TODO: (optional) additional reader configuration here

    // EXAMPLE: disable rarely used I2/5 to improve performance
    [scanner setSymbology: ZBAR_I25
                   config: ZBAR_CFG_ENABLE
                       to: 0];

2 个答案:

答案 0 :(得分:0)

更改此行

 [_webView stringByEvaluatingJavaScriptFromString:@"alert('Trigger the JS!');"]; 

作为

 [_webView stringByEvaluatingJavaScriptFromString:@"alert(\"Trigger the JS!\");"]; 

它对我有用。

答案 1 :(得分:0)

webViewDidFinishLoad: 在Web视图完成加载框架后发送。

- (void)webViewDidFinishLoad:(UIWebView *)webView

一样打电话
-(void)webViewDidFinishLoad:(UIWebView *)webView{

         [self.webView stringByEvaluatingJavaScriptFromString:@"alert('Trigger the JS!');"];


     }

此链接您了解清楚uiwebview-javascript-and-objective-c