英文:
Swift Protocol and Delegate movement Back through multiple classes
问题
以下是您提供的代码的翻译:
View Controller(视图控制器):
class ViewController: UIViewController, PostNumber {
func printNumber(number: Int) {
print("Number is \(number)")
}
override func viewDidLoad() {
super.viewDidLoad()
let thirdClass = ThirdClass()
thirdClass.delegate = self
let secondClass = SecondClass()
secondClass.addNumbers(firstNumber: 10, secondNumber: 5)
}
}
Second Class(第二类):
class SecondClass {
func addNumbers(firstNumber: Int, secondNumber: Int) {
let sum = firstNumber + secondNumber
let thirdClass = ThirdClass()
thirdClass.multiplyNumbersByTwo(number: sum)
}
}
Third Class(第三类):
protocol PostNumber {
func printNumber(number: Int)
}
class ThirdClass {
var delegate: PostNumber?
func multiplyNumbersByTwo(number: Int) {
let result = number * 2
print("test")
delegate?.printNumber(number: result)
}
}
请注意,代码中的print语句没有被正确翻译,因为它们不需要翻译。如果您需要进一步的帮助或有其他问题,请随时提问。
英文:
I have a View Controller that passes data to a second class.
That class passes data to a third class. I am trying to use protocol and delegate to get information back to the original View Controller. However, I can't get that function to fire.
(VC -> Class 2 -> Class 3). That is the flow of data. What am I doing wrong?
Here is my code.
View Controller:
class ViewController: UIViewController, PostNumber {
func printNumber(number: Int) {
print("Number is \(number)")
}
override func viewDidLoad() {
super.viewDidLoad()
let thirdClass = ThirdClass()
thirdClass.delegate = self
let secondClass = SecondClass()
secondClass.addNumbers(firstNumber: 10, secondNumber: 5)
}
}
Second Class:
class SecondClass {
func addNumbers(firstNumber: Int, secondNumber: Int) {
let sum = firstNumber + secondNumber
let thirdClass = ThirdClass()
thirdClass.multiplyNumbersByTwo(number: sum)
}
}
Third Class:
protocol PostNumber {
func printNumber(number: Int)
}
class ThirdClass {
var delegate: PostNumber?
func multiplyNumbersByTwo(number: Int) {
let result = number * 2
print("test")
delegate?.printNumber(number: result)
}
}
答案1
得分: 1
你需要在ViewController中初始化ThirdClass,然后将其传递给SecondClass。在下面的更新后的代码中,thirdClass实例是在ViewController中创建的,并且同一个实例被传递给secondClass。这确保了两个类都引用同一个ThirdClass实例,从而使委托回调能够正常工作。
视图控制器:
class ViewController: UIViewController, PostNumber {
override func viewDidLoad() {
super.viewDidLoad()
let thirdClass = ThirdClass()
thirdClass.delegate = self
let secondClass = SecondClass()
secondClass.thirdClass = thirdClass
secondClass.addNumbers(firstNumber: 10, secondNumber: 5)
}
func printNumber(number: Int) {
print("Number is \(number)")
}
}
第二类:
class SecondClass {
var thirdClass: ThirdClass?
func addNumbers(firstNumber: Int, secondNumber: Int) {
let sum = firstNumber + secondNumber
thirdClass?.multiplyNumbersByTwo(number: sum)
}
}
第三类:
protocol PostNumber {
func printNumber(number: Int)
}
class ThirdClass {
var delegate: PostNumber?
func multiplyNumbersByTwo(number: Int) {
let result = number * 2
delegate?.printNumber(number: result)
}
}
英文:
You have to initialize ThirdClass on ViewController and then pass that into SecondClass. In the updated code below , the thirdClass instance is created in the ViewController, and the same instance is passed to the secondClass. This ensures that both classes are referring to the same instance of ThirdClass, allowing the delegate callback to work correctly.
View Controller:
class ViewController: UIViewController, PostNumber {
override func viewDidLoad() {
super.viewDidLoad()
let thirdClass = ThirdClass()
thirdClass.delegate = self
let secondClass = SecondClass()
secondClass.thirdClass = thirdClass
secondClass.addNumbers(firstNumber: 10, secondNumber: 5)
}
func printNumber(number: Int) {
print("Number is \(number)")
}
}
Second Class:
class SecondClass {
var thirdClass: ThirdClass?
func addNumbers(firstNumber: Int, secondNumber: Int) {
let sum = firstNumber + secondNumber
thirdClass?.multiplyNumbersByTwo(number: sum)
}
}
Third Class:
protocol PostNumber {
func printNumber(number: Int)
}
class ThirdClass {
var delegate: PostNumber?
func multiplyNumbersByTwo(number: Int) {
let result = number * 2
delegate?.printNumber(number: result)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论