I have 2 UIViews 1st: ToolbarView: This is the code for protocol in 1st view:
public enum ToolBarActionType: String {
case style = "style"
case undo = "undo"
case redo = "redo"
}
public protocol ToolBarDelegate: AnyObject {
func toolBarButtonAction(actionType: ToolBarActionType)
}
@objc public class ToolbarView: UIView {
enum ToolbarItems: Int {
case style = 1
case undo = 2
case redo = 3
}
public weak var delegate: ToolBarDelegate?
//This method is called onClick of toolbar buttons
@objc func toolBarButtonAction(sender: UIButton) {
let style = ToolbarItems(rawValue: sender.tag)
switch style {
case .style :
delegate?.toolBarButtonAction(actionType: .insertionStyle)
case .undo :
delegate?.toolBarButtonAction(actionType: .undo)
case .redo :
delegate?.toolBarButtonAction(actionType: .redo)
default :
return
}
}
}
This is my 2nd view: TextView This is the code in textview:
import Toolbar
public class TextView: UIView, ToolBarDelegate {
private var toolbar = Toolbar()
public init() {
toolbar.delegate = self
}
//this method is not getting called in TextView.
public func toolBarButtonAction(actionType: ToolBarActionType) {
switch actionType {
case .style:
print("style is pressed")
case .undo:
print("style is pressed")
case .redo:
print("style is pressed")
}
}
}
Only the 1st views delegate method is called but not the 2nd views. Am I missing something here? Thank you!
CodePudding user response:
I think that super.init() need to call so you can use delegate method
CodePudding user response:
The delegate exists in ToolbarView
where as you're initialising Toolbar
object. Maybe try replacing
public class TextView: UIView, ToolBarDelegate {
private var toolbar = ToolbarView()
public init() {
super.init(frame: .zero)
toolbar.delegate = self
}
required init?(coder: NSCoder) {
super.init(coder: coder)
toolbar.delegate = self
}
//this method is not getting called in TextView.
public func toolBarButtonAction(actionType: ToolBarActionType) {
switch actionType {
case .style:
print("style is pressed")
case .undo:
print("undo is pressed")
case .redo:
print("redo is pressed")
}
}
}