2017-07-29 75 views
0

我在git集線器上發現了一個條形碼掃描器項目,我將其合併到我的應用程序link中。我正在使用Google圖書API來獲取有關我掃描的圖書的信息。發送信息到另一個視圖控制器

func getBookInfo(isbn: String) { 
    guard let url = URL(string: "https://www.googleapis.com/books/v1/volumes?q=isbn13:\(isbn)") else { 
     print("the url is not valid") 
     return 
    } 
    URLSession.shared.dataTask(with: url, completionHandler: {data, response, error -> Void in 
     guard error == nil else { 
      print(response) 
      print(error!.localizedDescription) 
      return 
     } 
     guard let data = data else { 
      print("no error but no data") 
      print(response) 
      return 
     } 
     guard let jsonResult = try? JSONSerialization.jsonObject(with: data, options: []) else { 
      print("the JSON is not valid") 
      return 
     } 
     if let arrayOfTitles = (jsonResult as AnyObject).value(forKeyPath: "items.volumeInfo.title") as? [String] { 
      self.BookName.text = "\(arrayOfTitles[0])" 
      print(self.BookName.text!) 
     } 
     if let arrayOfAuthors = (jsonResult as AnyObject).value(forKeyPath: "items.volumeInfo.authors") as? [[String]] { 
      self.Author.text = "\((arrayOfAuthors[0])[0])" 
      print(self.Author.text!) 
     } 
     if let arrayOfCategories = (jsonResult as AnyObject).value(forKeyPath: "items.volumeInfo.categories") as? [[String]] { 
      self.Category.text = "\((arrayOfCategories[0])[0])" 
      print(self.Category.text!) 
     } 
     if let arrayOfISBN13 = (jsonResult as AnyObject).value(forKeyPath: "items.volumeInfo.industryIdentifiers.identifier") as? [[String]] { 
      self.ISBN13.text = "\((arrayOfISBN13[0])[0])" 
      print(self.ISBN13.text!) 
     } 
     if let arrayOfISBN10 = (jsonResult as AnyObject).value(forKeyPath: "items.volumeInfo.industryIdentifiers.identifier") as? [[String]] { 
      self.ISBN10.text = "\((arrayOfISBN10[0])[1])" 
      print(self.ISBN10.text!) 
     } 
     if let arrayOfFormat = (jsonResult as AnyObject).value(forKeyPath: "items.volumeInfo.printType") as? [String] { 
      self.CoverType.text = "\(arrayOfFormat[0])" 
      print(self.CoverType.text!) 
     } 

    }).resume() 
} 

後,我掃描書籍,並已收到的信息,我想辭退具有條形碼掃描儀,並在顯示視圖控制器視圖控制器,我想顯示我的書的信息只是掃描。

extension MultipleImageViewController: BarcodeScannerCodeDelegate { 

    func barcodeScanner(_ controller: BarcodeScannerController, didCaptureCode code: String, type: String) { 


     if code.isEmpty { 

      let delayTime = DispatchTime.now() + Double(Int64(6 * Double(NSEC_PER_SEC)))/Double(NSEC_PER_SEC) 
      DispatchQueue.main.asyncAfter(deadline: delayTime) { 
       controller.resetWithError() 

      } 

     } 
     else{ 

      getBookInfo(isbn: code) 


      let delayTime = DispatchTime.now() + Double(Int64(6 * Double(NSEC_PER_SEC)))/Double(NSEC_PER_SEC) 
      DispatchQueue.main.asyncAfter(deadline: delayTime) { 
       controller.dismiss(animated: true, completion: nil) 

      } 
     } 

    } 
} 

然而,當條形碼掃描器視圖控制器被駁回,我不得不退出程序,然後再回來到應用程序,以便爲我的信息,在這我想它的視圖控制器來顯示。不用離開應用程序並返回來自條形碼掃描器的信息不會顯示在所需的視圖控制器中。

+0

這是什麼意思?「我必須退出應用程序,然後返回到應用程序,以便我的信息在視圖控制器中顯示出我想要的。」 – Shubham

+0

請您詳細說明代碼段沒有以預期的方式工作? – Shubham

+0

當我掃描書本代碼並找到該書時,我想關閉條形碼控制器,然後讓我的已發現信息顯示在現有的視圖控制器上,但它不會顯示,除非我按Home按鈕退出該應用程序,然後再次按下應用程序。然後顯示信息 – juelizabeth

回答

0

您的用戶界面沒有更新,因爲當視圖控制器已被加載時,該視圖控制器返回時,未調用方法viewDidLoad。相反,當你關閉子視圖控制器時,爲父項創建一個委託,該委託會被調用。有點像這樣:

class ParentViewController: UIViewController, BarcodeDelegate { 
    func presentChildViewController() { 
     let childViewController = ChildViewController() 
     childViewController.delegate = self 
     present(childViewController, animated: true, completion: nil) 
    } 

    func dismissedChildViewController(code: String) { 
     // update UI 
    } 
} 

class ChildViewController: UIViewController { 
    var delegate: BarcodeDelegate? 

    func dismiss() { 
     delegate.dismissedChildViewController(code: getCode()) 
     dismiss(animated: true, completion: nil) 
    } 
} 

protocol BarcodeDelegate { 
    func dismissedChildViewController(code: String) 
} 

很難真正理解問題是什麼,所以這可能是也可能不是你要找的。

+0

我認爲你是對的,我只是有最難的時間應用到我的代碼。我嘗試調用'barcodeScanner(_ controller:BarcodeScannerController,didCaptureCode code:String,type:String)'委託在viewdidload中,但我得到錯誤參數 – juelizabeth

+0

@juelizabeth,viewDidLoad不會從另一個道德提出的vc返回時調用。父母是孩子的委託,所以孩子一旦完成就調用委託方法。 – Dopapp

相關問題