2017-06-21 94 views
0

我是一個新的程序員,並在控制器之間傳遞數據掙扎,我知道這些問題在這些論壇中被問到,但上傳的代碼比我的複雜得多,我很難理解它會上傳我寫的幾行。我想讓兩名玩家在我的應用中輸入他們的名字,然後點擊開始遊戲。當開始遊戲按鈕被按下時,它將它們帶到一個新的視圖控制器,它的名字就像這樣Joe Bloggs vs John Doe,我可以實現這個如果我讓我的變量是全局的,但我的理解是這是不好的做法,所以當我嘗試編寫相同的代碼,保持視圖控制器內的變量它每次都通過零,我不確定我做錯了什麼?這是我的幾行代碼,我希望有人能回答我做錯了什麼?視圖控制器之間傳遞數據產生零

的ViewController:

import UIKit 

class ViewController: UIViewController { 

    var playerOneName: String! 
    var playerTwoName: String! 

    @IBOutlet weak var playerOneTextField: UITextField! 
    @IBOutlet weak var playerTwoTextField: UITextField! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    @IBAction func startGameButton(_ sender: Any) { 

     playerOneName = playerOneTextField.text! 
     playerTwoName = playerTwoTextField.text! 

    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "segue" { 

      let destinationVC = segue.destination as! GameViewController 
      destinationVC.playerOne.text = playerOneName 
      destinationVC.playerTwo.text = playerTwoName 
     } 
    } 

} 

GameViewController:

import UIKit 

class GameViewController: UIViewController { 

    @IBOutlet weak var playerOne: UILabel! 
    @IBOutlet weak var playerTwo: UILabel! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 

    } 

} 
+0

你檢查,如果文本字段被正確地映射到變量?在按鈕回調之前調用'prepare(for:sender:)'會怎麼樣? – gskbyte

+0

因爲當您執行destinationVC.playerOne.text = playerOneName時,'playerOne'是'nil'。換句話說,'IBOulet'尚未加載。創建一個將保存'playerOneName'值的'String'屬性。在'viewDidLoad()'設置'playerOne.text'。 – Larme

+0

在Objective-C中,但原因是一樣的:https://stackoverflow.com/questions/18137073/set-uitextfield-text-property-in-prepareforsegue或https://stackoverflow.com/questions/9576177/how-可以-I-設定的值對的一-的UILabel-以下-A-SEGUE – Larme

回答

0

,直到你的控制器的風景被你的店鋪沒有得到初始化。然而,在這之前就發生了這種情況。

最好的解決方案是將幾個字符串屬性放入GameViewController並使用它們接收來自第一個控制器的數據。然後,更新viewDidLoad中的標籤文字。

1

你不應該直接從另一個視圖控制器設置標籤的文本,但創建變量在GameViewController存儲標籤文本,更改SEGUE這些變量,然後更改標籤在你的目標控制器的viewDidLoad方法。見下面的代碼。

class GameViewController: UIViewController { 

    @IBOutlet weak var playerOne: UILabel! 
    @IBOutlet weak var playerTwo: UILabel! 

    var playerOneName:String? 
    var playerTwoName:String? 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     playerOne.text = playerOneName 
     playerTwo.text = playerTwoName 
     // Do any additional setup after loading the view. 

    } 

} 

並在視圖控制器:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "segue" { 

      let destinationVC = segue.destination as! GameViewController 
      destinationVC.playerOneName = playerOneName 
      destinationVC.playerTwoName = playerTwoName 
     } 
    } 
2

你控制器初始化之前,不能設置的@IBOutlets屬性。 您需要在第二個視圖控制器中存儲像String!這樣的值。例如,與他們在viewDidLoad方法中。

變化GameViewController代碼如下:

import UIKit 

class GameViewController: UIViewController { 
    @IBOutlet weak var playerOne: UILabel! 
    var playerOneName: String!  

    @IBOutlet weak var playerTwo: UILabel! 
    var playerTwoName: String! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     playerOne.text = playerOneName 
     playerTwo.text = playerTextName 
     // Do any additional setup after loading the view. 
    } 
} 

而且prepareViewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "segue" { 
     let destinationVC = segue.destination as! GameViewController 
     destinationVC.playerOneName = playerOneName 
     destinationVC.playerTwoName = playerTwoName 
    } 
} 

希望它可以幫助

相關問題