简体   繁体   English

在UIViewController上执行的UIView上的动态创建UIButton AddTarget

[英]Dynamic Creation UIButton AddTarget on UIView executed on UIViewController

Currently this code that executes a Tag Styled List, The issue remains when I want to try to pass the addTarget Action optionClicked to my UIViewController 目前,该代码执行一个标签样式化列表,问题仍然当我想尝试的addTarget行动optionClicked传递给我的UIViewController

DrawerView.swift DrawerView.swift


let menuOptions = ["Info", "Actions", "Users", "Patiens"]

        menuOptions.forEach({

            let button = UIButton()
            button.setTitle($0, for: .normal)

            if $0.contains(menuOptions[0]) {
                button.style(with: .filled)
            } else {
                button.style(with: .outlined)
                button.addTarget(self, action: #selector(optionClicked(_:)), for: .touchUpInside)
            }
            actionStackView.addArrangedSubview(button)
        })

DrawerController.swift DrawerController.swift


class DrawerController: UIViewController {

    var shareView = DrawerView()
    var viewModel: CarDetailViewModel?

    override func loadView() {
        shareView.viewModel = viewModel
        view = shareView
    }

    @objc func optionClicked(_ sender: UIButton) {

        let feedbackGenerator = UISelectionFeedbackGenerator()
        feedbackGenerator.selectionChanged()

        let optionClicked: String = sender.titleLabel?.text ?? "0"

        switch optionClicked {
        case "Actions": present(DrawerActionController(), animated: true, completion: nil)
        case "Notifications":
            let viewController = DrawerNotificationController()
            viewController.carDetailViewModel = viewModel
           present(viewController, animated: true, completion: nil)

        case "Patients":
            let viewUserController = DrawerUserController()
            viewUserController.carPath = "9000"
           present(viewUserController, animated: true, completion: nil)
        default: break
        }
    }
}

Tried button.addTarget(self, action: #selector(optionClicked(_:)), for: .touchUpInside) but did not work out. 尝试使用button.addTarget(self,action:#selector(optionClicked(_ :)),用于:.touchUpInside),但没有解决。

In the method button.addTarget(self, action: #selector(optionClicked(_:)), for: .touchUpInside) you need to provide pointer to the viewController which will receive the action. 在方法button.addTarget(self, action: #selector(optionClicked(_:)), for: .touchUpInside)您需要提供指向将接收动作的viewController的指针。

The easiest way in your case would be to create lazy variable DrawerView with DrawerController on the init and use the drawer controller in the button action. 在您的情况下,最简单的方法是使用init上的DrawerController创建惰性变量DrawerView并在按钮操作中使用抽屉控制器。

DrawerView.swift DrawerView.swift

class DrawerView: UIView {

    private unowned let drawerController: DrawerController

    init(drawerController: DrawerController) {
        self.drawerController = drawerController
    }

    ... wherever is your code placed ...

    let menuOptions = ["Info", "Actions", "Users", "Patiens"]

    menuOptions.forEach({

        let button = UIButton()
        button.setTitle($0, for: .normal)

        if $0.contains(menuOptions[0]) {
            button.style(with: .filled)
        } else {
            button.style(with: .outlined)
            button.addTarget(drawerController, action: #selector(DrawerController.optionClicked(_:)), for: .touchUpInside)
            actionStackView.addArrangedSubview(button)
        }
    })

    ....
}

It's important you use unowned or weak to prevent retain cycles and memory leaks 重要的是,您必须使用unownedweak unowned来防止保留周期和内存泄漏

To create the DrawerView you can use a lazy variable: 要创建DrawerView,可以使用惰性变量:

lazy var shareView: DrawerView = DrawerView(drawerController: self)

lazy will allow you to use self, you can also use optional and create the variable later, but that's basically what lazy does. lazy允许您使用self,也可以使用optional并在以后创建变量,但这基本上是lazy所做的。

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM