2016-10-10 133 views
0

我對Swift非常陌生,我正在製作這款迷你遊戲類型的應用程序,用於統計分數並更新視圖控制器。我想從視圖控制器將該分數傳遞給我創建的另一個外部彈出式視圖控制器。如何將數據從視圖控制器傳遞到我的彈出式視圖控制器(swift/ios)

@IBAction func Button7Tapped(_ sender: AnyObject) 
    { 
     if Index == 13 { 
      game.score += 1 
     } else { 
      let scorepopVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "finalScorePop") as! finalScoreViewController 

      self.addChildViewController(scorepopVC) 
      scorepopVC.view.frame = self.view.frame 
      self.view.addSubview(scorepopVC.view) 
      scorepopVC.didMove(toParentViewController: self) 
     } 
     updateGame() 
    } 

以上就是我爲我創造了外部彈出視圖控制器,其中也有一個分離.swift文件代碼。我將如何去取我的game.score並將其傳入我的Popup視圖控制器?

+0

如果解決了您的問題,您應該接受適當的答案。 – Majster

回答

0

最好使用故事板打開ViewController。在故事板中,右鍵單擊並拖動按鈕到第二個視圖控制器(您想要打開的視圖控制器)。

選擇您想要使用的segue類型。在你的情況下,我認爲現在模態將工作正常。

enter image description here

你會看到故事板兩個UIViewControllers之間的一條線。這是繼續。點擊它。在屬性檢查器中給出一個標識符。例如「myFirstSegue」。

enter image description here

然後在包含您的按鈕替代準備(爲的UIViewController的代碼:發件人:)。這個方法在準備繼續發生時被調用。當你點擊按鈕時,您可以訪問目標UIViewController,並可以訪問並設置它的屬性。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "myFirstSegue" { 
     if let vc = segue.destination as? MyViewController { 
      //here you set your data on the destination view controller 
      vc.myString = "Hello World" 
     } 
    } 
} 

注意,我們檢查標識,因爲從該視圖控制器到其他ViewControllers全力以赴塞格斯將調用準備(爲:發件人:)

+1

您正在過渡不添加子視圖控制器,就像他在他的代碼示例中那樣。 – Majster

0

在你finalScoreViewController迅速的文件中添加一個新的屬性。

final class FinalScoreViewController: UIViewController { 
    var score: Int? 


} 

然後在實例化時分配它。

@IBAction func Button7Tapped(_ sender: AnyObject) { 
    if Index == 13 { 
     game.score += 1 
    } else { 
     let scorepopVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "finalScorePop") as! finalScoreViewController 

     scorepopVC.score = game.score //THIS LINE 

     self.addChildViewController(scorepopVC) 
     scorepopVC.view.frame = self.view.frame 
     self.view.addSubview(scorepopVC.view) 
     scorepopVC.didMove(toParentViewController: self) 
    } 
    updateGame() 
} 
0

這很簡單,只要在你的finalScoreViewController添加屬性(如果您尚未這樣做)和 - 用於示例 - 稱之爲得分:

class finalScoreViewController: UIViewController { 

    var score: String? 

    // ... 

此行添加到Button7Tapped行動(你對finalScoreViewController的得分設置的值):

let scorepopVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "finalScorePop") as! finalScoreViewController 

     // add this line: 
     scorepopVC.score = "My score" 


     self.addChildViewController(scorepopVC) 
     scorepopVC.view.frame = self.view.frame 
     self.view.addSubview(scorepopVC.view) 
     scorepopVC.didMove(toParentViewController: self) 

最後,在finalScoreViewController

override func viewDidLoad() { 
    super.viewDidLoad() 

    if let scr = score { 
     print(scr) 
    } 
} 

希望有所幫助。

+1

附加說明:*命名約定*,您應該調用彈出類:「FinalScoreViewController」而不是「finalScoreViewController」(類命名的上部駝峯大小寫)。 –

+0

@Antonio Chan這裏是一個Swift風格的指南,你可能會發現有趣的https://github.com/raywenderlich/swift-style-guide –

相關問題