【问题标题】:Where to set delegate in swift?在哪里快速设置委托?
【发布时间】:2019-04-04 10:52:33
【问题描述】:

我已经建立了一个简单的 Swift 项目来尝试将我的头脑围绕在委托和协议上。目标是在两个类之间传递数据(SendingClass & ReceivingClass)。 SendingClass 中的两个按钮链接到委托,委托应该触发 ReceivingClass 中的符合协议的函数来执行。不幸的是,这不起作用,我怀疑它与我在哪里以及如何将 ReceivingClass 声明为代表有关。

感谢您的见解,我才刚刚开始!

我已尝试将代理设置在不同的位置(目前在 viewDidLoad 内,但无法正常工作)。

let vc = SendingClass()
vc.statusDelegate = self

SendingClass.swift

import UIKit


protocol StatusDelegate {
    func statusChanged(state: Bool, sender: String)
}

class SendingClass: UIViewController {

    var statusDelegate : StatusDelegate?


    @IBAction func button1Pressed(_ sender: UIButton) {
      statusDelegate?.statusChanged(state: true, sender: "Button 1")
    }

    @IBAction func button2Pressed(_ sender: UIButton) {
        statusDelegate?.statusChanged(state: false, sender: "Button 2")
    }

}

ReceivingClass.swift

import Foundation
import UIKit

class ReceivingClass: UIViewController, StatusDelegate {

    override func viewDidLoad() {
        let vc = SendingClass()
        vc.statusDelegate = self
    }

    func statusChanged(state: Bool, sender: String) {
        print("Sender = \(sender) , State = \(state)")
    }
}

预期:每次在 SendingClass 中按下按钮时,都应执行符合 ReceivingClass 协议的函数 (func statusChanged)。

实际:什么都没有发生

【问题讨论】:

  • 你如何展示你的SendingClass?在 IBActions 中,我猜你是从情节提要中完成的。
  • 你在哪里展示SendingClass
  • @Dávid Pásztor, @Rico Crescenzio 是的,按钮是通过一个简单的故事板呈现的。我已经用 print 语句测试了 IBactions,它们似乎工作正常。
  • @GCr 我的回答对你有帮助吗?

标签: ios swift delegates


【解决方案1】:
I am using this..
// create extension in your receiving class
extension ReceivingClass: PopUpVCDelegate {
   func statusChanged(state: Bool, sender: String) {
        print("Sender = \(sender) , State = \(state)")
    }
}

// on sending class, when you present your receiving class on any button click
eg.
let resultController = self.storyboard?.instantiateViewController(withIdentifier: "PopUpVCID") as? PopUpVC       

        resultController?.delegate = self
  self.present(resultController!, animated: true, completion: nil)

 //or if not have button add on viewdidload in receiving class

// here is full eg

How to get data from popup view controller to custom table view cell?

【讨论】:

    【解决方案2】:

    对于协议和委托,当您想将值从 2nd VC(由 1st 呈现或由 1st VC 推送)带到原始 1st VC 时,您可以使用它。 从您的代码中,我没有看到您展示或推动您的第二个 VC。这就是为什么它不起作用。希望我回答了你的疑问。

    但是,如果您仍想将价值从第 1 个 VC 带到第 2 个 VC。在第二个 VC 中,创建一个变量来接收它

    var ReceivedData = String()
    

    然后从你的第一个 VC,当你要推送它时,

    let vc = SendingClass()
    vc.ReceivedData = "Whatever you want it to receive"
    

    【讨论】:

    • 好的,所以协议/委托模式是为管理堆栈上的视图呈现而保留的?如果 ReceivingClass 实际上是模型,而不是 ViewController,那么使用这种模式是否合适。所以 ReceivingClass 只是监听 SendingClass 的变化然后响应(但不被呈现)。
    • erm ..确实没有得到你的问题...请支持任何有帮助的答案..特别是像我这样的新成员..哈哈
    【解决方案3】:

    如果您使用故事板转场,则视图控制器可能是从那里实例化的,因此您可能必须使用 prepareForSegue 并在 ReceivingClass 视图控制器中获取目标视图控制器(已为您实例化) :

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        super.prepare(for: segue, sender: sender)
    
        if let destination = segue.destination as? SendingClass {
            destination.delegate = self
        }
    }
    

    还要小心委托模式:委托属性应声明为weak 属性以避免保留循环

    weak var delegate: MyDelegate?
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-12
      • 2021-02-26
      • 1970-01-01
      • 2020-05-28
      • 1970-01-01
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多