【问题标题】:Accessing a constant from another view controller从另一个视图控制器访问常量
【发布时间】:2016-12-14 03:28:33
【问题描述】:

我在 firstViewController 类中有这个函数,它产生 itemID

 func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {

     // Check if the metadataObjects array is not nil and it contains at least one object.
    if metadataObjects == nil || metadataObjects.count == 0 {
        qrCodeFrameView?.frame = CGRect.zero
        messageLabel.text = "No QR/barcode is detected"
        return
    }
   //Get metadata object
let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
    if supportedCodeTypes.contains(metadataObj.type) {
    //if the found metadata is equal to the QR code metadata then update the status label's text and set the the bounds
        let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
        qrCodeFrameView?.frame = barCodeObject!.bounds

        if metadataObj.stringValue != nil {
        messageLabel.text = metadataObj.stringValue
            let itemID = metadataObj.stringValue
            print(itemID)


        }
    }
}

我想在另一个名为 secondViewController 的 viewController 中调用 itemID。如何从 secondViewController 中的 firstViewController 调用 itemID?

【问题讨论】:

    标签: ios swift uiviewcontroller swift3


    【解决方案1】:

    您只需将其传递给“prepareForSegue”。 只需确保在 secondviewcontroller 上将 itemId 声明为变量。

    override func prepare(for segue: UIStoryboardSegue, sender: Any?){
            if (segue.identifier == "secondViewControllerSeque") {
                let secondViewController = segue.destination as! SecondViewController
                secondViewController.itemID  = self.itemID 
            }
    
        }
    

    【讨论】:

      【解决方案2】:

      如果你从 FirstViewController 到 SecondViewController 你可以像这样传递数据。

      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
          if segue.identifier == "SecondViewControllerSegue" {
              let secondViewController = segue.destination as? SecondViewController
              if let itemID = itemID {
                  secondViewController.itemID = itemID
              }
          }
      }
      

      但不要忘记在 FirstViewController 中创建 itemID 变量,而是像这样编写代码

      var itemID: String?
      
      
      if metadataObj.stringValue != nil {
          messageLabel.text = metadataObj.stringValue
              itemID = metadataObj.stringValue
          }
      

      【讨论】:

        【解决方案3】:

        firstViewController 类需要一个成员变量 itemID。然后第二个 viewcontroller 应该可以引用它。

        现在 itemID 是您的 captureOutpur 函数中的一个局部变量。 一旦函数完成运行,这就会消失。而是将其分配给成员。

        【讨论】:

          【解决方案4】:

          如果你使用的是segue,你只需要这样做:

          FirstViewController(注意:名称使用大写字母):

          override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
              if segue.identifier == "ShowNextView" {        // the identifier is what you named it
                  if let vc = segue.destination as? SecondViewController {
                      vc.itemID = itemID                     // itemID is accessible here, probably global
                  }
              }
          }
          

          SecondViewController:

          var itemID:String
          
          func viewWillAppear() {
              // code to use itemID value
          }
          

          【讨论】:

            【解决方案5】:

            只需执行 3 个步骤,假设您要从 ViewControllerA to ViewControllerB: 传递数据

            1.在ViewControllerA and ViewControllerB之间创建一个segue

            2.在属性检查器中使用标识符命名segue

            3.覆盖prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) at ViewControllerA

            override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
                if segue.identifier == "MySegueID" {
                    if let destination = segue.destinationViewController as? ViewControllerB {
                        destination.myInformation = self.myInformation
                    }
                }
            }
            

            【讨论】:

              【解决方案6】:

              首先在 FirstViewController 中声明一个变量。

              var itemID: String?
              

              并在导航到 SecondViewController 之前调用您的函数将值分配给该变量。

              if metadataObj.stringValue != nil {
                  self.itemID = metadataObj.stringValue
                 }
              

              现在,还要在 SecondViewController 中声明 itemIdForSVC 公共变量,

              public var itemIdForSVC: String?
              

              如果您使用 Storyboard,则在导航时传递这样的参数,

              override func prepare(for segue: UIStoryboardSegue, sender: Any?){
                      if (segue.identifier == "secondViewControllerSeque") {
                          let secondViewController = segue.destination as! SecondViewController
                          secondViewController.itemIdForSVC  = self.itemID 
                      }
              
                  }
              

              或者,如果您以编程方式完成所有操作,则在导航时像这样传递,

              let secondViewConttoller = SecondViewController()
              secondViewController.itemIdForSVC  = self.itemID
              self.navigationController?.pushViewController(secondViewConttoller, animated: true)
              

              【讨论】:

              • 谢谢,我正在以编程方式完成所有工作。我应该在 FirstViewController 的哪个位置声明我似乎对此有问题的代码 let secondViewConttoller = SecondViewController() secondViewController.itemIdForSVC = self.itemID self.navigationController?.pushViewController(secondViewConttoller, animated: true)
              • 您应该将此代码添加到您想要转到下一页的事件中,即 onButtonTapped 事件。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-11-28
              • 2017-03-23
              相关资源
              最近更新 更多