簡體   English   中英

iOS Swift 協議和委托

[英]iOS Swift protocol & delegate

為什么我的代表團不工作?

在我的示例中,操作按鈕在單擊時起作用,但由於某種原因,它在我的第二個 controller 中沒有達到 didAction function。

protocol HomeControllerDelegate: class {
    func didAction()
}

class HomeController: UIViewController {
    
    weak var delegate: HomeControllerDelegate?
    
    private let actionButton: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("Action", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.backgroundColor = .black
        button.setHeight(50)
        button.setWidth(100)
        button.addTarget(self, action: #selector(handleAction), for: .touchUpInside)
        return button
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        view.addSubview(actionButton)
        actionButton.centerX(inView: view)
        actionButton.centerY(inView: view)
    }
    
    @objc func handleAction() {
        print("DEBUG: Handle Action Button delegate here....")
        delegate?.didAction()
    }
}
class SecondController: UIViewController {
    
    let homeController = HomeController()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        homeController.delegate = self
    }
}

extension SecondController: HomeControllerDelegate {
    func didAction() {
        print("DEBUG: In SecondController - didAction()")
    }
}

非常感謝您的指導,它使我了解了基礎知識以及要研究的內容。

我創建了以下內容來解決我的問題。

一個容器 controller 實例化兩個 ViewControler 並將它們設置為 childVC

然后在 FirstChildVC 上創建了一個協議,SecondChildVC 是委托

現在一切正常,我明白了很多。

class ContainerController: UIViewController {
    
    let secondChildVC = SecondChildVC()
    let firstChildVC = FirstChildVC()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        addFirstChildVC()
        addSecondChildVC()
    }
    
    func addSecondChildVC(){
        addChild(secondChildVC)
        view.addSubview(secondChildVC.view)
        secondChildVC.didMove(toParent: self)
        setSecondChildVCConstraints()
    }
    
    func addFirstChildVC(){
        addChild(firstChildVC)
        view.addSubview(firstChildVC.view)
        firstChildVC.delegateActionButton = secondChildVC
        firstChildVC.didMove(toParent: self)
        setFirstChildVCConstraints()
    }
    
    func setFirstChildVCConstraints() {
        firstChildVC.view.translatesAutoresizingMaskIntoConstraints = false
        firstChildVC.view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20).isActive = true
        firstChildVC.view.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = true
        firstChildVC.view.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20).isActive = true
        firstChildVC.view.heightAnchor.constraint(equalToConstant: 200).isActive = true
    }
    
    func setSecondChildVCConstraints() {
        secondChildVC.view.translatesAutoresizingMaskIntoConstraints = false
        secondChildVC.view.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -20).isActive = true
        secondChildVC.view.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = true
        secondChildVC.view.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20).isActive = true
        secondChildVC.view.heightAnchor.constraint(equalToConstant: 200).isActive = true
    }
}
protocol FirstChildVCDelegate: class {
    func didAction(data: String)
}

class FirstChildVC: UIViewController {
    
    weak var delegateActionButton: FirstChildVCDelegate!
    
    private let actionButton: UIButton = {
        let button = UIButton(type: .system)
        let buttonHeight = 30
        button.setTitle("Button", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.backgroundColor = .black
        button.layer.cornerRadius = CGFloat(buttonHeight / 2)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.heightAnchor.constraint(equalToConstant: CGFloat(buttonHeight)).isActive = true
        button.translatesAutoresizingMaskIntoConstraints = false
        button.widthAnchor.constraint(equalToConstant: 100).isActive = true
        button.addTarget(self, action: #selector(handleAction), for: .touchUpInside)
        return button
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemPink
        configureActionButtonView()
    }
    
    func configureActionButtonView() {
        view.addSubview(actionButton)
        actionButton.translatesAutoresizingMaskIntoConstraints = false
        actionButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        actionButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    }
    
    @objc func handleAction() {
        delegateActionButton.didAction(data: "Button Pressed")
    }
}

class SecondChildVC: UIViewController {
    
    private let actionButtonLabel: UILabel = {
        let label = UILabel()
        label.text = "test"
        label.font = .systemFont(ofSize: 18)
        label.textColor = .white
        label.isHidden = true
        return label
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemPurple
        configureActionButtonLabel()
    }
    
    func configureActionButtonLabel() {
        view.addSubview(actionButtonLabel)
        actionButtonLabel.translatesAutoresizingMaskIntoConstraints = false
        actionButtonLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        actionButtonLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    }
}
extension SecondChildVC: FirstChildVCDelegate {
    func didAction(data: String) {
        actionButtonLabel.isHidden.toggle()
        actionButtonLabel.text = data
    }
}

初始 State

State 一次按下按鈕

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM