【问题标题】:sending information to another view controller向另一个视图控制器发送信息
【发布时间】:2017-07-29 14:02:31
【问题描述】:

我在 git hub 上找到了一个条码扫描器项目,我将它并入了我的应用程序 link。我正在使用谷歌图书 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)

            }
        }

    }
}

但是,当条码扫描器视图控制器被关闭时,我必须退出应用程序,然后返回应用程序,以便我的信息在视图控制器中显示为我想要的。如果不离开应用程序并返回,我从条形码扫描仪收到的信息不会显示在所需的视图控制器中。

【问题讨论】:

  • 这是什么意思“我必须退出应用程序然后返回应用程序才能让我的信息显示在我想要的视图控制器中。”
  • 请您详细说明哪段代码未按预期方式工作?
  • 当我扫描图书代码并找到图书时,我想关闭条形码控制器,然后将找到的信息显示在现有视图控制器上,但除非我按主页,否则它不会显示按钮退出应用程序,然后再次按下应用程序。然后显示信息
  • 我认为您在关闭控制器后忘记更新 UI 以获取接收到的信息。
  • @Shubham 我该怎么做?

标签: ios swift google-books


【解决方案1】:

您的 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)
}

很难真正理解问题所在,因此这可能是也可能不是您要查找的内容。

【讨论】:

  • 我认为你是对的,我只是很难将它应用到我的代码中。我尝试在 viewdidload 中调用 barcodeScanner(_ controller: BarcodeScannerController, didCaptureCode code: String, type: String) 委托,但在参数中出现错误
  • @juelizabeth,当您从另一个道德提出的 vc 中返回时,不会调用 viewDidLoad。父级是子级的委托,所以一旦完成,子级就会调用委托的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多