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

标签 ios swift uiview uiviewcontroller uiview-hierarchy

目前这段代码执行标签样式列表,当我想尝试将 addTarget 操作 optionClicked 传递给我的 UIViewController

时,问题仍然存在

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


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
        }
    }
}

尝试了 button.addTarget(self, action: #selector(optionClicked(_:)), for: .touchUpInside) 但没有成功。

最佳答案

在方法 button.addTarget(self, action: #selector(optionClicked(_:)), for: .touchUpInside) 中,您需要提供指向将接收操作的 viewController 的指针。

在您的情况下,最简单的方法是在初始化时使用 DrawerController 创建惰性变量 DrawerView,并在按钮操作中使用抽屉 Controller 。

抽屉 View .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)
        }
    })

    ....
}

使用unownedweak 来防止保留循环和内存泄漏很重要

要创建 DrawerView,您可以使用惰性变量:

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

lazy 将允许您使用 self,您也可以使用 optional 并稍后创建变量,但这基本上就是 lazy 所做的。

希望对您有所帮助!

关于ios - 在 UIViewController 上执行的 UIView 上动态创建 UIButton AddTarget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57859161/

相关文章:

ios - 如何在 IOS Apps Swift 中处理强制关闭

swift - 唯一的日历条目标识符

swift - 查找和修改 subview 的困难

ios - 将事件添加到以编程方式生成的 UIView

ios - 尝试显示 ViewController 后出现奇怪的错误

ios - 有没有办法在 Info.plist 文件的其他地方引用 CFBundleIdentifier ?

ios - 具有 NULL 值的 NSDateFormatter 部分结果

ios - 从另一个类访问 UI 操作

ios - 关于 XCode (Swift) 中选择器的快速问答

iphone - ipad:呈现具有缩小效果的 View 并具有放大效果的删除