【发布时间】:2020-01-11 12:35:29
【问题描述】:
当前执行标记样式列表的代码,当我想尝试将 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) 但没有成功。
【问题讨论】:
-
检查界面生成器。可能是您将按钮与视图控制器连接,然后从视图控制器中删除了代码,但错过了连接。
-
@AlexanderVolkov 我没有使用情节提要。都是代码。
-
看起来目标是视图?您需要将操作的目标设置为视图控制器,或者设置路径以便视图可以将操作传达给视图控制器(例如,通过委托)。
-
@3ddpaz 然后检查您的代码在哪里。看起来
self的目标是视图控制器。将其更改为您的视图,您有optionClicked方法。 -
@AlexanderVolkov 将 uiviewcontroller 中的 self 作为委托添加到 uiview 并将其添加到 addTarget 上的 self 没有发生任何事情。
标签: ios swift uiview uiviewcontroller uiview-hierarchy