Multiple times segment usage in different screen, btn == sender comparison not return true.
muhammedkarakul opened this issue · comments
Hi, I use this custom segmented control in my project on different screens. First screen okey it works but when I go to second screen, my other custom segmented control won't work. I debug "if btn == sender" condition and i see btn and sender have same title but not have same address. Its mean buttons are same but not equal because they are different objects in memory. Why am I having such a problem like this. Any idea?
Hello @muhammedkarakul. Can you send your code?
Hello @faganello60, thanks for quick response
i define segmentedControl like below;
private(set) lazy var segmentControl: LCSegmentedControl = { LCSegmentedControl(titles: AuthTypes.allCases.map { $0.rawValue }, textColor: .silver, selectedTextColor: .white, lineColor: .white) }()
and I makes some changes on CustomSegmentedControl class;
`import UIKit
import UIKit
protocol LCSegmentedControlDelegate:class {
func segmentedControl(_ segmentedControl: LCSegmentedControl, didIndexChanged index: Int)
}
class LCSegmentedControl: UIView {
//MARK:- Properties
private lazy var segmentTitles: [String] = []
private var buttons = UIButton
private lazy var selectorView = UIView()
private var textColor: UIColor? = .lightGray
private var selectorViewColor: UIColor? = .red
private var selectorTextColor: UIColor? = .red
private var selectorWidth: Int {
Int(frame.width) / self.segmentTitles.count
}
var normalTitleFont: UIFont = .light14
var selectedTitleFont: UIFont = .medium16
weak var delegate: LCSegmentedControlDelegate?
private(set) var selectedIndex = 0
private lazy var stackView: UIStackView = {
let stackView = UIStackView(arrangedSubviews: buttons)
stackView.axis = .horizontal
stackView.alignment = .fill
stackView.distribution = .fillEqually
return stackView
}()
//MARK:- Lifecycle
convenience init(titles: [String], textColor: UIColor? = .lightGray, selectedTextColor: UIColor? = .black, lineColor: UIColor? = .red, normalTitleFont: UIFont = .light14, selectedTitleFont: UIFont = .medium16) {
self.init()
self.segmentTitles = titles
self.textColor = textColor
self.selectorTextColor = selectedTextColor
self.selectorViewColor = lineColor
self.normalTitleFont = normalTitleFont
self.selectedTitleFont = selectedTitleFont
}
//MARK:- Helper methods
override func draw(_ rect: CGRect) {
super.draw(rect)
self.backgroundColor = .clear
updateView()
}
private func setButtonTitles(segmentTitles:[String]) {
self.segmentTitles = segmentTitles
self.updateView()
}
private func setIndex(index:Int) {
buttons.forEach({ $0.setTitleColor(textColor, for: .normal) })
let button = buttons[index]
selectedIndex = index
button.setTitleColor(selectorTextColor, for: .normal)
let selectorPosition = frame.width/CGFloat(segmentTitles.count) * CGFloat(index)
UIView.animate(withDuration: 0.2) {
self.selectorView.frame.origin.x = selectorPosition
}
}
@objc func buttonAction(sender:UIButton) {
for (buttonIndex, btn) in buttons.enumerated() {
btn.setTitleColor(textColor, for: .normal)
btn.titleLabel?.font = normalTitleFont
if btn.isEqual(sender) {
let selectorPosition = frame.width / CGFloat(segmentTitles.count) * CGFloat(buttonIndex)
selectedIndex = buttonIndex
delegate?.segmentedControl(self, didIndexChanged: buttonIndex)
UIView.animate(withDuration: 0.3) {
self.selectorView.frame.origin.x = selectorPosition
}
btn.setTitleColor(selectorTextColor, for: .normal)
btn.titleLabel?.font = selectedTitleFont
}
}
}
private func createButton() {
buttons.removeAll()
subviews.forEach({$0.removeFromSuperview()})
for buttonTitle in segmentTitles {
let button = UIButton(type: .system)
button.setTitle(buttonTitle, for: .normal)
button.addTarget(self, action:#selector(LCSegmentedControl.buttonAction(sender:)), for: .touchUpInside)
button.setTitleColor(textColor, for: .normal)
buttons.append(button)
}
buttons.first?.setTitleColor(selectorTextColor, for: .normal)
buttons.first?.titleLabel?.font = selectedTitleFont
}
private func updateView() {
createButton()
configSelectorView()
prepareLayout()
}
private func prepareLayout() {
addSubviews(stackView, selectorView)
stackView.snp.makeConstraints { (make) in
make.edges.equalToSuperview()
}
selectorView.snp.makeConstraints { (make) in
make.height.equalTo(2)
make.top.equalTo(snp.bottom)
make.width.equalTo(selectorWidth)
}
}
private func configSelectorView() {
selectorView.backgroundColor = selectorViewColor
}
}
`
Change isEqual to ==
I try == and === but they not works either.