我有正從一個類中的一個簡單的變量到另一個問題,它是超越非常令人沮喪:/ ...如何訪問另一個類中的變量? SWIFT代碼
這裏是交易,我有兩個視圖控制器類命名爲:ViewController.swift
和ViewController2.swift
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
}
當我從這個ViewController打印bagCenter時,它工作正常,並給我一個正確的值。所以bagCenter
變量我想以某種方式傳遞給ViewController2.swift
。
這是我從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()
}
但我做到這一點時,它總是導致一個0,我沒有得到普遍呈現ViewController1了袋的實際COORDS。
您在ViewController2 –
創建視圖控制器的新實例,我將如何去不同的看法?在此先感謝:) –