我无法实现委托模式。

huangapple go评论67阅读模式
英文:

I can't realise delegate pattern

问题

I can provide a translation of the provided code without the code parts:

在SWIFT中的初学者

我有2个视图控制器(ViewController),并希望在第二个视图控制器(secondVC)中显示我在第一个视图控制器(firstVC)中输入的内容。我查看了一些类似的主题,但没有找到相关内容,似乎与Storyboard有些相似,但我不懂Storyboard。

我的第一个视图控制器(firstVC):

protocol DataDelegate {
    func getWord(word: String)
}

class MainViewController: UIViewController {

    let mainView = MainView()
    var delegate: DataDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view = mainView
        addTargets()
    }

    func addTargets() {
        mainView.startButton.addTarget(self, action: #selector(startGame), for: .touchUpInside)
    }

    @objc func startGame() {

        guard mainView.wordTf.text!.count > 7 else {
            print("Slishkom korotkoe slovo")
            return
        }

        let vc = GameViewController()
        vc.modalPresentationStyle = .fullScreen
        delegate?.getWord(word: mainView.wordTf.text!)
        self.present(vc, animated: true)
    }

}

我的第二个视图控制器(secondVC):

class GameViewController: UIViewController {

    let gameView = GameView()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view = gameView
        addTargets()
    }

    func addTargets() {

        gameView.backButton.addTarget(self, action: #selector(dismissSelf), for: .touchUpInside)
    }

    @objc func dismissSelf() {
        self.dismiss(animated: true)
    }

}

extension GameViewController: DataDelegate {
    func getWord(word: String) {
        gameView.bigWord.text = word
    }
}

我没有找到类似的内容,求帮助!

英文:

Beginner in SWIFT

I have 2 ViewController and want to show in secondVC(some Label) what I wrote in firstVC(some textfield) I checked similar topics but found nothing, there is something similar with storyboard, but I don't know storyboard.

my firstVC:

protocol DataDelegate {
    func getWord(word:String)
}

class MainViewController: UIViewController {
     
    let mainView = MainView()
    var delegate: DataDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view = mainView
        addTargets()
    }

    func addTargets() {
        mainView.startButton.addTarget(self, action: #selector(startGame), for: .touchUpInside)
    }
    
    @objc func startGame() {
        
        guard mainView.wordTf.text!.count > 7 else {
            print("Slishkom korotkoe slovo")
            return
        }
        
        let vc = GameViewController()
        vc.modalPresentationStyle = .fullScreen
        delegate?.getWord(word: mainView.wordTf.text!)
        self.present(vc, animated: true)
    }

}


my secondVC:

class GameViewController: UIViewController {
    
    let gameView = GameView()
    

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view = gameView
        addTargets()
    }
    
    func addTargets() {
       
        gameView.backButton.addTarget(self, action: #selector(dismissSelf), for: .touchUpInside)
    }
    
    @objc func dismissSelf() {
        self.dismiss(animated: true)
        

    }

}

extension GameViewController: DataDelegate {
    func getWord(word: String) {
        gameView.bigWord.text = word
    }
    
    
}

I didn't find anything similar, pls help!

答案1

得分: 0

你几乎肯定不想为此使用协议/代理模式...

一般来说,当你创建你的控制器(GameViewController)时,你希望传递数据,然后再呈现它。

在你的GameViewController中添加一个属性来接收数据(在这种情况下是一个字符串):

class GameViewController: UIViewController {

    var gameWord: String = ""

    // 你的其余代码

}

然后,在MainViewController中点击一个按钮来调用startGame()时:

@objc func startGame() {
    
    guard theWord = mainView.wordTf.text, theWord.count > 7 else {
        print("Slishkom korotkoe slovo")
        return
    }
    
    let vc = GameViewController()
    vc.modalPresentationStyle = .fullScreen

    // 在这里“传递”数据
    vc.gameWord = theWord

    self.present(vc, animated: true)
}

现在,你已经将在wordTf中输入的文本设置为你的游戏视图控制器中的gameWord,你可以按照你的需要使用它。

英文:

You almost certainly do NOT want to use protocol/delegate pattern for this...

In general, you want to pass data to your presented controller (your GameViewController) when you create it, and then present it.

Add a property to your GameViewController to receive the data (a string, in this case):

class GameViewController: UIViewController {

    var gameWord: String = ""

    // all the rest of your code

}

Then, when you tap a button in MainViewController to call startGame():

@objc func startGame() {
    
    guard theWord = mainView.wordTf.text, theWord.count > 7 else {
        print("Slishkom korotkoe slovo")
        return
    }
    
    let vc = GameViewController()
    vc.modalPresentationStyle = .fullScreen

    // "pass" the data here
    vc.gameWord = theWord

    self.present(vc, animated: true)
}

You now have the text entered into wordTf set as gameWord in your game view controller, and you can use it however you want.

huangapple
  • 本文由 发表于 2023年2月26日 23:22:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定