2016-07-05 259 views
-1

我有一個UIWebView主要UIViewController它工作正常。我用這個命令來發送數據,並再次這一切都很好:從另一個視圖調用Web視圖控制器

[self.dWebView stringByEvaluatingJavaScriptFromString: myString]; 

的問題是,我添加了另一個UIViewControllerQRViewController),採用QR標籤讀取數據,我想送它以同樣的方式。 這是我的代碼在QRViewController但什麼也沒有發生:

ViewController* _viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
[_viewController.dWebView stringByEvaluatingJavaScriptFromString: qrString]; 

是否有可能做這種方式?有什麼遺漏嗎?

在此先感謝。

回答

1

添加的NSString * STR對象添加到您的ViewController

傳爲:_viewController.str = qrString;

[self.dWebView stringByEvaluatingJavaScriptFromString:self.str];

這將工作

+0

謝謝,我也試過了。我解僱了QRViewController [self dismissViewControllerAnimated:YES completion:nil];我期望viewWillAppear或viewDidAppear在主ViewController將被調用來發送字符串,但他們不是。 – MorZa

+0

你有沒有試過在塊中寫代碼? [self dismissViewControllerAnimated:YES completion:^ { [self.dWebView stringByEvaluatingJavaScriptFromString:myString]; }]; –

+1

否則讓委託人回傳數據。它一定會奏效 –

1

你正在創建相同UIViewController類的第二個實例調用此代碼:

ViewController* _viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 

_viewController不指着一個在屏幕上可見,這是一個新的當前未顯示的副本。

您需要獲取當前顯示的前一個實例的句柄並將數據傳回給它。這將取決於你如何構建這個應用程序。

例如:

  • 如果存在有模式,你將需要獲得self.presentingViewController
  • 如果您使用navigationController,則可以訪問其viewControllers屬性以獲取前一個的句柄。
相關問題