gpt4 book ai didi

ios - 如何在 Swift 中检查子类中使用了哪个 ViewController

转载 作者:行者123 更新时间:2023-11-30 11:26:11 27 4
gpt4 key购买 nike

我正在制作一款有 2 种游戏模式(红色模式和蓝色模式)的射击游戏。这两种模式是 ViewController,有 subView(UIView 子类)和游戏结果 View ,显示您获得了多少分数。
但根据游戏模式的不同,会有一些差异。我尝试制作“ResultView Class”并想让这两种游戏模式拥有该类。但我不知道如何根据游戏模式单独处理。

理想:

  1. 创建“ResultView 类”
  2. 在“showResultView”函数中制作每种游戏模式(RedView 和 BlueView)的 ResultView
  3. 根据游戏模式进行一些单独的处理

(现在我在每个游戏模式下都制作“resultView”,即使内容几乎相同”)

如何解决?

//ViewController for RedMode
class RedGameViewController: UIViewController {

var gameStatus: status!
var redView: RedView!

override init(frame: CGRect) {
super.init(frame: frame)

redView = RedView.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 568))
self.view.addSubview(redView)
redView.redGameViewController = self
//.....
}

enum status: String {
case isPlaying = "isPlaying"
case isPausing = "isPausing"
case isGameOver = "isGameOver"
}

class RedView: UIView {

weak var redGameViewController: RedGameViewController!

override init(frame: CGRect) {
super.init(frame: frame)
}
//....
@objc func showResultView(){
var resultView = UIImageView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 568))
resultView.isUserInteractionEnabled = true
resultView.image = UIImage(named:"resultView_BG")!
self.addSubview(resultView)

resultScoreLabel.text = String(score)
resultBackToTopBtn.isHidden = false
if redViewController.gameStatus == .isGameOver {
print("isGameOver")
}else{
print("Clear")
}
}
}
//ViewController for BlueMode
class BlueGameViewController: UIViewController {

var gameStatus: status!
var blueView: BlueView!

override init(frame: CGRect) {
super.init(frame: frame)

blueView = BlueView.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 568))
self.view.addSubview(blueView)
blueView.blueGameViewController = self
//.....
}
enum status: String {
case isPlaying = "isPlaying"
case isPausing = "isPausing"
case isGameOver = "isGameOver"
}

class BlueView: UIView {

weak var blueGameViewController: BlueGameViewController!
override init(frame: CGRect) {

super.init(frame: frame)
}
//....
@objc func showResultView(){
var resultView = UIImageView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 568))
resultView.isUserInteractionEnabled = true
resultView.image = UIImage(named:"resultView_BG")!
self.addSubview(resultView)

//Different from showResultView in RedView
resultScoreLabel.text = String(score*10)
resultBackToTopBtn.isHidden = true
if blueViewController.gameStatus == .pausing {
print("isPausing")
}else{
print("Clear")
}
}
}

最佳答案

实现这一点的一个好方法是使用协议(protocol)。

选项 1:

假设您有一个名为 ColoredView 的协议(protocol),其中包含您的逻辑方法

protocol ColoredView {
func showResult(/* add parameters if you need them */)
}

在名为 ResultViewController 的 UIViewController 中,您将拥有一个能够调用逻辑函数的 ColoredView,例如

class ResultViewController {
var coloredView: UIView!

func showResultView() {
guard let coloredView = self.coloredView as? ColoredView else {
// This is not a coloredView
return
}
coloredView.showResult()
}
}

由此,只需创建两个实现协议(protocol)的 View

class RedView: UIView, ColoredView {
func showResult() {
// Insert Red logic here
}
}

class BlueView: UIView, ColoredView {
func showResult() {
// Insert Blue logic here
}
}

然后只需创建 ResultViewController,并使用 RedView 或 BlueView 作为其 colorView。

选项 2:

您可以使用类似的协议(protocol)来分隔变量

protocol ColoredView {
func getScoreText(from score: Int) -> String
func getPrintText(for status: Status) -> String
var isHidden: Bool
}

就像选项 1 一样

func showResult() {
if let coloredView = self.coloredView as? ColoredView {
resultScoreLabel.text = coloredView.getScoreText(from: score)
resultBackToTopBtn.isHidden = coloredView.isHidden
print(coloredView.getPrintText(for: gameStatus)
}
}

class RedView: UIView, ColoredView {
func getScoreText(from score: Int) -> String {
// Insert Red logic here
}

var isHidden = false

func getPrintText(for status: Status) -> String {
// Insert Red logic here
}
}

class BlueView: UIView, ColoredView {
func getScoreText(from score: Int) -> String {
// Insert Blue logic here
}

var isHidden = true

func getPrintText(for status: Status) -> String {
// Insert Blue logic here
}
}

希望有帮助;)

关于ios - 如何在 Swift 中检查子类中使用了哪个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50715313/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com