2017-07-29 152 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

相关问题