2016-03-26 9 views
0

Bir viewcontroller'da bir barkod-çizgeyim ve ikinci bir viewcontroller'da bir kullanıcı arabirimim var. İkinci viewcontroller'da ilk viewcontroller yakalayıcısında oluşturulan dizeyi nasıl alabilirim. Birkaç şeyi denedim ve nslog ile test ettim ama her zaman dize değeri için (nil) gösterir. Bundan kurtulmama yardım et.Görünüm denetleyici1'den viewcontroller2'ye aktarın ve uiwebview'in metin kutusunda barkod taraması (String) gösterme

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 (sadece

-(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

webViewDidFinishLoad:

#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

İşte benim kodudur. 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

Mülk kullanmış mısınız? –

+0

lütfen .h dosyasını –

+0

yükleyin ve yükleyin. –

cevap

0

Tamam, şimdi çalışıyor. String'i geçmek ve diğer ViewController'ımda yakalamak için NSUserDefaults kullandım. Cheers

İlgili konular