我在一个viewcontroller中有一个barcodescanner,在第二个viewcontroller中有一个uiwebview。如何在第二个viewcontroller中获取在第一个viewcontroller catch中创建的字符串。我尝试了几件事并用nslog测试了它,但它总是显示我(nil)字符串值。请帮帮我。
这是我的代码:
igViewController.m:
#import <AVFoundation/AVFoundation.h>
#import "igViewController.h"
#import "ViewController.h"
@interface igViewController () <AVCaptureMetadataOutputObjectsDelegate>
{
AVCaptureSession *_session;
AVCaptureDevice *_device;
AVCaptureDeviceInput *_input;
AVCaptureMetadataOutput *_output;
AVCaptureVideoPreviewLayer *_prevLayer;
UIView *_highlightView;
}
@end
@implementation igViewController
@synthesize detectionString;
@synthesize delegate;
@synthesize thedetected;
- (void)viewDidLoad
{
[super viewDidLoad];
_highlightView = [[UIView alloc] init];
_highlightView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin;
_highlightView.layer.borderColor = [UIColor greenColor].CGColor;
_highlightView.layer.borderWidth = 3;
[self.view addSubview:_highlightView];
_label = [[UILabel alloc] init];
_label.frame = CGRectMake(0, self.view.bounds.size.height - 40, self.view.bounds.size.width, 40);
_label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
_label.backgroundColor = [UIColor colorWithWhite:0.15 alpha:0.65];
_label.textColor = [UIColor whiteColor];
_label.textAlignment = NSTextAlignmentCenter;
_label.text = @"(nichts)";
[self.view addSubview:_label];
_session = [[AVCaptureSession alloc] init];
_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
_input = [AVCaptureDeviceInput deviceInputWithDevice:_device error:&error];
if (_input) {
[_session addInput:_input];
} else {
NSLog(@"Error: %@", error);
}
_output = [[AVCaptureMetadataOutput alloc] init];
[_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[_session addOutput:_output];
_output.metadataObjectTypes = [_output availableMetadataObjectTypes];
_prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
_prevLayer.frame = self.view.bounds;
_prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:_prevLayer];
[_session startRunning];
[self.view bringSubviewToFront:_highlightView];
[self.view bringSubviewToFront:_label];
}
-(void) viewWillAppear:(BOOL)animated{
if(detectionString!=nil){
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *top = [UIApplication sharedApplication].keyWindow.rootViewController;
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"Controller1"];
[top presentViewController:vc animated:YES completion:nil];
}
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
CGRect highlightViewRect = CGRectZero;
AVMetadataMachineReadableCodeObject *barCodeObject;
detectionString = nil;
NSArray *barCodeTypes = @[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode];
for (AVMetadataObject *metadata in metadataObjects) {
for (NSString *type in barCodeTypes) {
if ([metadata.type isEqualToString:type])
{
barCodeObject = (AVMetadataMachineReadableCodeObject *)[_prevLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata];
highlightViewRect = barCodeObject.bounds;
detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
setSomeNumber:[(AVMetadataMachineReadableCodeObject *)metadata stringValue];
break;
}
}
if (detectionString != nil)
{
_label.text = detectionString;
// ViewController *otherViewController=[[ViewController alloc] init];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"Controller1"];
ViewController *otherViewController=[[ViewController alloc] init];
[otherViewController barcodeverarbeiten:detectionString];
NSLog(@"detectionstringssssss %@", detectionString);
[self presentViewController:vc animated:YES completion:nil];
break;
}
else
_label.text = @"(nichts)";
}
_highlightView.frame = highlightViewRect;
}
- (void)setSomeNumber:(NSString *)tempString {
_label.text = tempString;
}
-(NSString *) thedetected{
return _label.text;
}
@end
ViewController.m(仅限webViewDidFinishLoad
-(void) webViewDidFinishLoad:(UIWebView *)awebView{
NSLog(@"detectionstring %@", otherViewController1.thedetected);
if(otherViewController1.thedetected != nil){ //denk an self. ... bei igviewcontroller
[self.Webseite stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementsByName('SEARCH_STRING_CONTENT')[0].value = '%@';",otherViewController1.thedetected]];
NSString *evalStr = [NSString stringWithFormat:@"setTimeout( function(){document.getElementsByName(SEARCH_STRING_CONTENT')[0].focus();},1000);"];
[self.Webseite stringByEvaluatingJavaScriptFromString:evalStr];
[self.Webseite stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('buttonLinks searchButton withValue')[0].click();"];
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if ([awebView canGoBack] == YES){
[backButton setEnabled:YES];
}else{
[backButton setEnabled:NO];
}
if([awebView canGoForward] == YES){
[forwardButton setEnabled:YES];
}else{
[forwardButton setEnabled:NO];
}
}
igViewController.h:
#import <UIKit/UIKit.h>
@class ViewController;
@interface igViewController : UIViewController{
ViewController *delegate;
@public
UILabel *_label;
NSString *detectionString;
//NSString *tempString;
}
@property (nonatomic, strong)NSString *detectionString;
@property (nonatomic, strong) ViewController *delegate;
@property (nonatomic, strong) NSString *thedetected;
//@property (nonatomic, strong)NSString *tempString;
- (void)setSomeNumber:(NSString *)tempString;
@end
ViewController.h:
#import <UIKit/UIKit.h>
#import "FavoritenController.h"
@class igViewController;
@interface ViewController : UIViewController <UITextFieldDelegate, UIWebViewDelegate, UIActionSheetDelegate>
{
UIWebView *Webseite;
UIBarButtonItem *backButton;
UIBarButtonItem *forwardButton;
UITextField *textField;
UIBarButtonItem *refreshButton;
NSString* urlBeforeEditing;
UIBarButtonItem *barcodescan;
igViewController *otherViewController1;
FavoritenController* favoritenController;
/* ..*/
}
@property (nonatomic, retain) IBOutlet UIBarButtonItem *barcodescan;
@property (nonatomic, strong) igViewController *otherViewController1;
@property (nonatomic, retain) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *onlinetauscher;
@property (nonatomic, retain) IBOutlet UIWebView *Webseite;
@property ( nonatomic, retain) IBOutlet UIBarButtonItem *refreshButton;
@property ( nonatomic, retain) IBOutlet UIBarButtonItem *backButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *forwardButton;
- (IBAction)onlinetauscher:(UIBarButtonItem *)sender;
- (IBAction)pressRefresh:(UIBarButtonItem *)sender;
//- (IBAction)search:(id)sender;
- (IBAction)zeigeMedikamente:(id)sender;
- (IBAction)hinzufuegen:(id)sender;
- (void)barcodeverarbeiten:(NSString*)barcodepzn;
@end
答案 0 :(得分:0)
好的,它现在正在工作。我使用NSUserDefaults传递String并在我的其他ViewController中捕获它。干杯