首页 > 解决方案 > 从 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")
}
}

标签: iosswiftuikit

解决方案


您需要 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)))

推荐阅读