2015-08-08 96 views
0

我試圖從this answer實現解決方案,但是我只是無法使其工作。我得到「未解決的標識符的使用......」因爲我試圖訪問的變量..Swift:從不同的swift文件中訪問其他類的變量

我有一個loginViewController.swift當有人登錄在獲取用戶名和密碼..

import UIKit 

類loginViewController:UIViewController的{

@IBOutlet weak var userEmailTextField: UITextField! 
@IBOutlet weak var userPasswordTextField: UITextField! 


override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

@IBAction func loginButtonTapped(sender: AnyObject) { 

    let userEmail = userEmailTextField.text; 
    let userPassword = userPasswordTextField.text; 

然後,我有一個PostService.swift文件從MySQL數據庫中獲取數據。應該使用的人登錄後才能訪問相關數據的用戶名..

import Foundation 

類PostService {

var settings:Settings! 

let userEmail = "[email protected]"; //This is hard coded but needs to come from the loginViewController 
let userPassword = "1234";    //This is hard coded but needs to come from the loginViewController 

我已嘗試和一些其他的建議,我似乎只是沒有能夠得到它的工作..請幫助..

謝謝提前。

+0

你能分享你正在使用的實際代碼嗎?你如何實例化PostService類?您需要全局聲明變量,您可以使用self關鍵字來訪問它們。 –

回答

1

你可以只聲明

var userEmail = ""; 
var userPassword = ""; 

一個類(也沒關係斯威夫特類)。然後,丟棄let

@IBAction func loginButtonTapped(sender: AnyObject) { 
    userEmail = userEmailTextField.text; 
    userPassword = userPasswordTextField.text; 

將存儲的變量,你只能夠使用在PostService類。

但是,LoginViewController和PostService之間真的沒有連接嗎?你必須調用PostService函數 ...也許你可以傳遞變量嗎?

+0

謝謝格洛芬德爾。我爲此奮鬥了幾天...變數保持空白。然後我意識到,我需要點擊登錄按鈕將值傳遞給變量...重置我的iOS模擬器,登錄和巴姆! –