ios - 发送信息到另一个 View Controller

标签 ios swift google-books

我在 git hub 上找到了一个条形码扫描仪项目,并将其合并到我的应用程序中 link 。我正在使用 google books 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()
}

在我扫描书籍并收到信息后,我想关闭具有条形码扫描仪的 View Controller ,并在出现的 View Controller 中显示我刚刚扫描的书籍的信息。

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)

            }
        }

    }
}

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

最佳答案

您的 UI 未更新,因为当 View Controller 已加载而返回时,不会调用方法 viewDidLoad。相反,为父级创建一个委托(delegate),当您关闭 subview Controller 时将调用该委托(delegate)。有点像这样:

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

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

关于ios - 发送信息到另一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45385643/

相关文章:

ios - 使用 Safari View Controller 登录时的 Swift iOS Facebook SDK 循环

javascript - 将 ajax 响应属性放入 Google 图书封面图像的变量中

ios - 找出系统默认的单位类型(升/加仑)

ios - 将每次迭代打印到标签

android - 使用 Google 图书 API V1 搜索特定类别的图书

ios - Stripe IOS SDK 不匹配 AllResponseFields?

c# - CVPixelBufferGetBaseAddressOfPlane

iphone - 使用嵌套对象检测缺失的 JSON 键

ios - Flutter iOS - MediaQuery.of(context).size.width 没有给出以像素为单位的宽度