xcode 如何从另一个类访问变量?SWIFT代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38723081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:06:25  来源:igfitidea点击:

How to access variable from one class in another? Swift Code

xcodevariablesuiviewcontrollerglobaltransfer

提问by IPhonick 9

I am having an issue getting one simple variable from one class to another and it is beyond extremely frustrating :/...

我在将一个简单的变量从一个类获取到另一个类时遇到问题,这非常令人沮丧:/...

Here is the deal I have two view controller classes named: ViewController.swiftand ViewController2.swift

这里是我有两个视图控制器类命名的交易:ViewController.swiftViewController2.swift

ViewController.swiftis linked to a storyboard that has sort of an inventory bag that I have placed which is just an image. Then there is an @IBAction for when you click on the bag it opens up and the second storyboard pops into view. This is controlled by ViewController2.swift. All I am looking to do is simply pass the center of the bag image from ViewController to ViewController2 but I can't seem to get it to work any help would be greatly appreciated.

ViewController.swift链接到一个故事板,其中有一个我放置的库存袋,它只是一个图像。然后有一个@IBAction,当你点击它打开的袋子时,第二个故事板就会出现在视图中。这是由 控制的ViewController2.swift。我想要做的只是将包图像的中心从 ViewController 传递到 ViewController2,但我似乎无法让它工作,任何帮助将不胜感激。

class ViewController: UIViewController {
    @IBOutlet weak var InventoryBag: UIImageView!
    var bagCenter:CGPoint = CGPoint(x: 0, y: 0)

    override func viewDidLoad() {
        super.viewDidLoad()

        @IBAction func InventoryButtonTapped(sender: UIButton) {
            self.InventoryBag.image = UIImage(named: "backpackimageopen")
            bagCenter = self.InventoryBag.center

        func transferViewControllerVariables() -> (CGPoint){
            return bagCenter
            }

When I print the bagCenter from this ViewController it works properly and gives me a correct value. So the bagCentervariable I would like to somehow pass over to ViewController2.swift.

当我从这个 ViewController 打印 bagCenter 时,它可以正常工作并给我一个正确的值。所以bagCenter我想以某种方式传递给ViewController2.swift.

Here is what I tried from ViewController2.swiftbut it never seems to work and always gives me a 0 rather than the actual value.

这是我尝试过的,ViewController2.swift但它似乎永远不会起作用,并且总是给我一个 0 而不是实际值。

class ViewController2: UIViewController {

@IBOutlet weak var HideButton: UIButton!
@IBOutlet weak var InventoryCollection: UICollectionView!



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    //Load the center of the inventory bag to this view controller to align the hide button.
    var bagCenter = ViewController().transferViewControllerVariables()
}

But when I do this it always results in a 0 and I don't get the actual coords of the bag that are showing up in ViewController1.

但是当我这样做时,它总是导致 0 并且我没有得到在 ViewController1 中显示的包的实际坐标。

采纳答案by Mohammed Shakeer

Create a following variable in ViewController2

在 ViewController2 中创建以下变量

var previousViewController: ViewController!

Add following line of code in your ViewController Class

在您的 ViewController 类中添加以下代码行

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){

    if segue.destinationViewController .isKindOfClass(ViewController2){
      let vc2 = segue.destinationViewController as! ViewController2
        vc2.previousViewController = self
    }
}

Now in viewDidLoadmethod of ViewController2you can access bagCenter like below:

现在viewDidLoadViewController2您可以像下面这样访问 bagCenter:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.


    var bagCenter = previousViewController.transferViewControllerVariables()
}

回答by Anil Kumar

Try this in view Controller

在视图控制器中试试这个

class ViewController: UIViewController { 

 @IBOutlet weak var InventoryBag: UIImageView!
    var bagCenter:CGPoint = CGPoint(x: 0, y: 0)

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func InventoryButtonTapped(sender: AnyObject) {
        self.InventoryBag.image = UIImage(named:"MAKEUP_SHARE.jpg")
        bagCenter = self.InventoryBag.center
        performSegueWithIdentifier("Go", sender: self)
    }


    // Give a segue identifier in storyboard
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "yoursegueidentifier" {
            let dvc =  segue.destinationViewController as? ViewController2
            dvc!.bagCenter = bagCenter
        }
    }
}

and in view controller2

并在视图控制器2中

class ViewController2: UIViewController {
    var bagCenter:CGPoint!
    override func viewDidLoad() {
        super.viewDidLoad()
        print(bagCenter)
    }
}