我在git hub上找到了一个条形码扫描程序项目,我将其整合到我的应用程序authenticate with limited priveleges中。我正在使用谷歌图书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()
}
然而,当条形码扫描仪视图控制器被解除时,我必须退出应用程序,然后返回应用程序,以便我的信息显示在我想要的视图控制器中。在不离开应用程序并返回的情况下,我从条形码扫描仪收到的信息不会显示在所需的视图控制器中。
答案 0 :(得分:0)
您的UI未更新,因为在该视图控制器已经加载时返回该视图控制器时,不会调用方法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)
}
很难真正理解问题所在,所以这可能是也可能不是你正在寻找的东西。