从另一个视图控制器访问常量

时间:2016-12-14 03:28:33

标签: ios swift uiviewcontroller swift3

我在class firstConController中有这个函数,它产生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?

6 个答案:

答案 0 :(得分:0)

你只需要在“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 
        }

    }

答案 1 :(得分:0)

如果您从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
    }

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

如果您使用的是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
}

答案 4 :(得分:0)

只需按照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
        }
    }
}

答案 5 :(得分:0)

首先在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)