ios - 从 View Controller 访问 subView 而无需额外的委托
问题描述
我有一个“视图控制器”和一个“主视图”,现在我有一个“子视图”,它创建一个自定义视图,该视图返回一个开关和一个与之相关的标签,我将其中几个自定义视图添加到我的主视图中. 我的问题:是否有可能设置按钮目标并仅在父视图中设置委托来访问这些子视图?还是他们每个人都需要自己的代表?
子视图:
import UIKit
class RowView: UIView {
var title: String
var isOn: Bool
init(title: String, isOn: Bool) {
self.title = title
self.isOn = isOn
super.init(frame: .zero)
setupViews()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let myLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 14)
label.textColor = .black
label.textAlignment = .center
return label
}()
public let mySwitch:UISwitch = {
let mySwitch = UISwitch()
mySwitch.layer.borderColor = UIColor.white.cgColor
mySwitch.layer.borderWidth = 0.8
mySwitch.layer.cornerRadius = 14
// mySwitch.tag = num
return mySwitch
}()
func setupViews() {
myLabel.text = title
mySwitch.isOn = isOn
addSubview(myLabel)
addSubview(mySwitch)
myLabel.anchor(top: topAnchor, leading: leadingAnchor, bottom: nil, trailing: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
mySwitch.anchor(top: nil, leading: nil, bottom: nil, trailing: trailingAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
mySwitch.centerYAnchor.constraint(equalTo: myLabel.centerYAnchor).isActive = true
}
// this is the suggested size for this view
override var intrinsicContentSize: CGSize {
return CGSize(width: 350, height: 31)
}
}
父视图:
import UIKit
protocol settingDelegate: AnyObject {
func removeAllFavorites()
//func activateNotification(isOn: Bool)
//func allowNotificationAlert(isOn: Bool)
}
class SettingView: UIView {
//MARK: - Properties
var delegate: settingDelegate?
var notifView: UIView?
var alertView: UIView?
let deleteButton: UIButton = {
let button = UIButton(type: .system)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
button.setTitle("Delete Favorite Quotes", for: .normal)
button.backgroundColor = UIColor.red
button.setTitleColor(.white, for: .normal)
button.layer.cornerRadius = 5
button.titleLabel?.adjustsFontSizeToFitWidth = true // adjust button text to the size
button.titleLabel?.minimumScaleFactor = 0.5 // make it 50% smaller at max
button.contentEdgeInsets = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
button.addTarget(self, action: #selector(deleteHandler), for: .touchUpInside)
button.setContentHuggingPriority(UILayoutPriority(1000), for: .horizontal)
return button
}()
//MARK: - Initializers
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//MARK: - Helper Methods
func setupViews() {
backgroundColor = .white
notifView = RowView(title: "Show day's quote as a notification", isOn: false)
alertView = RowView(title: "Ring an alert when notification is played", isOn: false)
guard let notifView = notifView, let alertView = alertView else { return }
let switchStack = UIStackView(arrangedSubviews: [notifView, alertView])
switchStack.axis = .vertical // stackVer.axis = .vertical
switchStack.distribution = .equalCentering
switchStack.alignment = .leading
switchStack.spacing = 20
let stack = UIStackView(arrangedSubviews: [switchStack, deleteButton])
stack.axis = .vertical
stack.distribution = .equalSpacing
stack.alignment = .center
stack.spacing = 20
addSubview(stack)
stack.anchor(top: safeAreaLayoutGuide.topAnchor, leading: leadingAnchor, bottom: safeAreaLayoutGuide.bottomAnchor, trailing: trailingAnchor, paddingTop: 20, paddingLeft: 10, paddingBottom: 20, paddingRight: 10, width: 0, height: 0)
}
@objc func deleteHandler() {
guard let delegate = delegate else { return }
delegate.removeAllFavorites()
}
@objc func switchChanged(mySwitch: UISwitch) {
print("changed")
}
}
解决方案
您需要 1 名代表并通过tag
notifView = RowView(title: "Show day's quote as a notification", isOn: false)
alertView = RowView(title: "Ring an alert when notification is played", isOn: false)
notifView.tag = 10
alertView.tag = 11
- - 你可以做
notifView.addGestureRecognizer(UITapGestureRecognizer(target: self.delegate!, action: #selector(self.delegate!.methodClick)))
推荐阅读
- react-native - 当另一个视图被聚焦时显示一个视图?
- swift - 使用 StoreKitTest 模拟失败的事务
- python - Pygame不断崩溃
- java - 如何解决 spring-boot 应用程序中无法加载驱动程序类:org.h2.Driver?
- javascript - 使用 js2py 从 Python 调用 JavaScript 函数会导致 ReferenceError: document is not defined
- python - 如果机器人单独在语音通道中,则使机器人断开连接 discord.py
- docker - Docker - 在图像之间共享层
- java - ParseException 引起的 Java Gson JsonSyntaxException: Unparsable date
- python - Python - 获取原始鼠标输入
- image - 在 Matlab 中从 png 文件制作 gif