第一个View Controller上的按钮可以正常工作。
第一控制人:GoalsVC
class GoalsVC: UIViewController {
let topView: UIView = { ... }()
let topViewTitleLabel: UILabel = { ... }()
let topViewAddButton: UIButton = {
...
// Add actions
button.addTarget(self, action: #selector(addGoalPressed), for: .touchUpInside)
return button
}()
let goalsTableView: UITableView = { ... }()
let firstGoalLabel: UILabel = { ... }()
override func viewDidLoad() {
super.viewDidLoad()
...
setupLayoutTopView()
...
}
func setupLayoutTopView() {
view.addSubview(topView)
...
topView.addSubview(topViewTitleLabel)
...
topView.addSubview(topViewAddButton)
...
}
func setupTableView() {
...
}
// Actions
@objc private func addGoalPressed() {
performSegue(withIdentifier: CREATE_GOAL_SEGUE, sender: nil)
} }
按下按钮后,该动作按预期方式工作,并且对下一个视图控制器进行了排序,但是在下一个视图控制器上,我向该按钮添加了动作,但没有触发该动作。完全没有反应。
第二个视图控制器:
class CreateGoalVC: UIViewController {
let topView: UIView = { ... }()
let topViewTitleLabel: UILabel = { ... }()
let topViewUnwindButton: UIButton = {
...
// Add actions
button.addTarget(self, action: #selector(unwindPressed(_:)), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
setupLayoutTopView()
}
func setupLayoutTopView() {
self.view.addSubview(topView)
...
topView.addSubview(topViewTitleLabel)
...
topView.addSubview(topViewUnwindButton)
...
}
// Actions
@objc func unwindPressed(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
我还测试了什么: 如果我执行新View Controller的实例并显示它而不是执行segue,则按钮将按预期工作。
@objc private func addGoalPressed() {
let vc = CreateGoalVC()
present(vc, animated: true)
}
简而言之:
感谢您的阅读:)