gpt4 book ai didi

ios - Swift 委托(delegate) - 新手 3 ViewController

转载 作者:行者123 更新时间:2023-11-30 13:49:24 25 4
gpt4 key购买 nike

所以这是我的问题。我从 Swift 和 C# 开始了我的旅程,我正在尝试以最简单的方式学习委托(delegate)。

我创建了一个 xcode 项目,在其中设置了 3 个 View Controller 。我创建了 2 个 Segue:

  1. 我可以从firstViewController (firstVC) 转到secondViewController (secondVC)
  2. 我可以从firstViewController转到thirdViewController(thirdVC)

应用程序用途:

  1. 从第一个VC转到第二个VC,点击一个按钮,关闭 View 并根据点击的按钮更新标签。 -- 这个很简单。

  2. 从第一个 VC 转到第三个 VC,点击那里唯一的按钮,关闭 View 并使用一些简单的文本更新与之前相同的标签 - 这也完成了。

  3. 如果我点击 ThirdVC 中的按钮,我想更改 SecondVC 中的标签

第三个对我来说并不容易。

代码如下:

第一个 View Controller :

import UIKit

class FirstViewController: UIViewController, SecondViewControllerDelegate, ThirdViewControllerDelegate {

@IBOutlet weak var answerLabel: UILabel!

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

}

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

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "firstToSecondSegue" {
let secondVC = segue.destinationViewController as! SecondViewController
secondVC.delegate = self
} else if segue.identifier == "firstToThirdSegue" {
let thirdVC = segue.destinationViewController as! ThirdViewController
thirdVC.delegate = self
}
}

@IBAction func answerButtonTapped(sender: UIButton) {
if sender.tag == 0 {
performSegueWithIdentifier("firstToSecondSegue", sender: self)
} else if sender.tag == 1 {
performSegueWithIdentifier("firstToThirdSegue", sender: self)
}
}

func giveTheAnswer(yes: Bool) {
if yes {
print("Yes is the answer")
self.answerLabel.text = "I do like chicken!"
} else {
print("No is the answer")
self.answerLabel.text = "I do not like chicken!"
}
}

func iWantPie(kindOfPie: String, disableButton: Bool) {
if kindOfPie == "Apple pie" {
print("Textlabel will change to applepie.")
self.answerLabel.text = "Fu*k chicken. Let's eat apple pie!"
}
if disableButton {
print("DisableButton set to true")
self.answerLabel.backgroundColor = UIColor.redColor()
}
}
}

第二个 View Controller :

protocol SecondViewControllerDelegate {
func giveTheAnswer(yes: Bool)}


class SecondViewController: UIViewController, ThirdViewControllerDelegate {

@IBOutlet weak var theOnlyLabel: UILabel!
@IBOutlet weak var yesButton: UIButton!
@IBOutlet weak var noButton: UIButton!

var delegate: SecondViewControllerDelegate?

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 answerButtonPressed(sender: UIButton) {

if sender.tag == 0 {
//Yes tapped
self.delegate?.giveTheAnswer(true)
dismissViewControllerAnimated(true, completion: nil)
} else if sender.tag == 1 {
//No Tapped
self.delegate?.giveTheAnswer(false)
dismissViewControllerAnimated(true, completion: nil)
}
}

func iWantPie(kindOfPie: String, disableButton: Bool) {
print("Function iWantPie in secondViewController is Running")
if disableButton == true {
print("DisableButton running")
self.theOnlyLabel.text = "Pie was tapped"
}

}}

第三 View Controller :

protocol ThirdViewControllerDelegate {
func iWantPie(kindOfPie: String, disableButton: Bool)}

class ThirdViewController: UIViewController {

var delegate: ThirdViewControllerDelegate?


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 piePressed(sender: UIButton) {
self.delegate?.iWantPie("Apple pie", disableButton: true)
dismissViewControllerAnimated(true, completion: nil)

}}

最佳答案

请通过在 if else 条件中添加断点来检查以下代码是否正常工作。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "firstToSecondSegue" {
let secondVC = segue.destinationViewController as! SecondViewController
secondVC.delegate = self
} else if segue.identifier == "firstToThirdSegue" {
let thirdVC = segue.destinationViewController as! ThirdViewController
thirdVC.delegate = self
}
}

还要检查您是否为 Storyboard 中的 segue 提供了正确的 segue 标识符。“firstToSecondSegue”和“firstToThirdSegue”

关于ios - Swift 委托(delegate) - 新手 3 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34487180/

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