我有两个 View Controller ,viewcontroller 1 和 viewcontroller 2。viewController 1 包含一个 webview。当我单击 Web View 内的链接时,我将 View Controller 2 推到 View Controller 1 的顶部。在 viewcontroller 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];
最佳答案
更改此行
[_webView stringByEvaluatingJavaScriptFromString:@"alert('Trigger the JS!');"];
作为
[_webView stringByEvaluatingJavaScriptFromString:@"alert(\"Trigger the JS!\");"];
它对我有用。
关于javascript - 无法在 UIWebView 中执行 javascript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21447231/