我有一個class1,它有很多變量,並且在一個類中也有一個函數。我從另一個類class2.function調用該函數,但是我無法訪問變量class1的。從另一個類被調用的函數中訪問變量
這裏是示例代碼
class ViewController: UIViewController {
var Flat:String?
var Flong:String?
var Tlat:String?
var Tlong:String?
override func viewDidLoad() {
super.viewDidLoad()
Flat = "flat value";
Flong="flong value";
Tlat="Tlat value";
Tlong="tlong value";
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func calculation()
{
print("origin_lat new\(Flat)")
print("origin_lng new\(Flong)")
print("dest_lat new\(Tlat)")
print("dest_lng new\(Tlong)")
}
}
我從另一個類Collectionviewcell點擊功能
var mycontroller : ViewController = ViewController()
mycontroller.calculation()
爲什麼我不能訪問值誰能幫我打電話的計算方法?
功能http://stackoverflow.com/questions/24072766/accessing-variables-from-another-viewcontroller-迅速 –
嘗試使用協議。 –