在 UIViewController 上执行的 UIView 上动态创建 UIButton AddTarget
Dynamic Creation UIButton AddTarget on UIView executed on UIViewController
当前执行标签样式列表的代码,当我想尝试将 addTarget 操作 optionClicked 传递给时,问题仍然存在我的 UIViewController
DrawerView.swift
let menuOptions = ["Info", "Actions", "Users", "Patiens"]
menuOptions.forEach({
let button = UIButton()
button.setTitle([=10=], for: .normal)
if [=10=].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,并在按钮操作中使用抽屉控制器。
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([=10=], for: .normal)
if [=10=].contains(menuOptions[0]) {
button.style(with: .filled)
} else {
button.style(with: .outlined)
button.addTarget(drawerController, action: #selector(DrawerController.optionClicked(_:)), for: .touchUpInside)
actionStackView.addArrangedSubview(button)
}
})
....
}
请务必使用 unowned
或 weak
来防止保留循环和内存泄漏
要创建 DrawerView,您可以使用惰性变量:
lazy var shareView: DrawerView = DrawerView(drawerController: self)
lazy 将允许您使用 self,您也可以使用 optional 并稍后创建变量,但这基本上就是 lazy 所做的。
希望对您有所帮助!
当前执行标签样式列表的代码,当我想尝试将 addTarget 操作 optionClicked 传递给时,问题仍然存在我的 UIViewController
DrawerView.swift
let menuOptions = ["Info", "Actions", "Users", "Patiens"]
menuOptions.forEach({
let button = UIButton()
button.setTitle([=10=], for: .normal)
if [=10=].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,并在按钮操作中使用抽屉控制器。
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([=10=], for: .normal)
if [=10=].contains(menuOptions[0]) {
button.style(with: .filled)
} else {
button.style(with: .outlined)
button.addTarget(drawerController, action: #selector(DrawerController.optionClicked(_:)), for: .touchUpInside)
actionStackView.addArrangedSubview(button)
}
})
....
}
请务必使用 unowned
或 weak
来防止保留循环和内存泄漏
要创建 DrawerView,您可以使用惰性变量:
lazy var shareView: DrawerView = DrawerView(drawerController: self)
lazy 将允许您使用 self,您也可以使用 optional 并稍后创建变量,但这基本上就是 lazy 所做的。
希望对您有所帮助!