![](/img/trans.png)
[英]Swift: Call a method from ViewController1 after dismissing ViewController2
[英]Transfer String from viewcontroller1 to viewcontroller2 and show barcode scan(String) in textbox of uiwebview
我在一個viewcontroller中有一個條形碼掃描儀,在另一個viewcontroller中有一個uiwebview。 我如何才能在第二個ViewController中捕獲在第一個ViewController中創建的字符串。 我嘗試了幾件事,並使用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
好的,現在正在工作。 我使用NSUserDefaults傳遞字符串,並將其捕獲到另一個ViewController中。 干杯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.