How can I fix action button after update Xcode12?

My action buttons stopped working after Xcode update (Xcode 12). When I clicked the buttons, I saw that the functions did not working (like there is no button in there). I could not understand why. How can I fix these functions?

Here my action button function:
Code Block
@objc func addAction(sender: UIButton) {
let indexPath = IndexPath(row: sender.tag, section: 0)
let cell = tableView.cellForRow(at: indexPath) as! customCell
cell.count += 1
cell.countBasketLabel.text = "x\(cell.count)"
let detailGelen = detailsModel[indexPath.row]
selectedItemName.append(detailGelen.itemDetailName!)
selectedItemPrice.append(detailGelen.itemDetailPrice!)
UserDefaults.standard.set(selectedItemName, forKey: "urunadi")
UserDefaults.standard.set(selectedItemPrice, forKey: "urunfiyat")
tableView.reloadData()
}


Here my button code:
Code Block
let addButton: UIButton = {
let button = UIButton(type: .system)
button.setImage(UIImage(named: "add"), for: .normal)
button.tintColor = .black
button.layer.cornerRadius = 5
button.addTarget(self, action: #selector(testdetail.addAction(sender:)), for: .touchUpInside)
return button
}()

@klo

Seems like a bug to me, what do you think @OOper?

Same as you. I'm not sure if Apple's engineer wants to call it a bug, but at least, this sort of significant change should be documented.
You should better send a feedback to Apple.
This thread is exceptionally difficult to follow, but I will echo that I believe I am seeing the same issue in our app. There are numerous instances throughout our app where we have UITableViewCell subclasses that embed buttons within their contentView AND also allow selection such that didSelectRow... will get triggered when the larger cell content is tapped.

As has been described by other posters, the way these buttons/cells are set up has not changed since we began compiling against Xcode 12, but it does seem that their underlying behavior has. Previously, the button taps would be accepted as expected; however, now, the button taps are ignored and the tap goes through to the cell itself.

Here's what I have determined so far:
  • As suggested by other posters, this is in some way tied to contentView.isUserInteractionEnabled. According to the documentation for that property, it is still supposed to default to true.

  • This issue can be worked around by simply accessing contentView.isUserInteractionEnabled - like by calling: print("\(contentView.isUserInteractionEnabled)")

  • The workaround will only work when the access is done outside of the initializer. I have tried numerous orderings and combinations, and it seems that if I directly access this property from within the code block of the initializer, it has no effect. However, if the access is made within a function called by the initializer, it does work.

A sample is attached below to further demonstrate the issue and my workaround:

Code Block swift
protocol MyCellDelegate: class {
    func didTapButtonInCell(_ cell: MyTableViewCell)
}
class MyTableViewCell: UITableViewCell {
    weak var delegate: MyCellDelegate?
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
// Adding either of these calls here does NOT work:
        // contentView.isUserInteractionEnabled = true
        // print("\(contentView.isUserInteractionEnabled)")
        setupMyButton()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupMyButton() {
// Adding either of these calls here DOES work:
       // contentView.isUserInteractionEnabled = true
       // print("\(contentView.isUserInteractionEnabled)")
        let button: UIButton = {
            $0.setTitle("Tap me!", for: .normal)
            return $0
        }(UIButton(type: .system))
        button.addTarget(self, action: #selector(didTapButton(_:)), for: .touchUpInside)
        // Add the button to the `contentView` and lay it out accordingly...
    }
    @objc private func didTapButton(_ sender: UIButton) {
        delegate?.didTapButtonInCell(self)
    }
}







had this problem too. turns out I was adding subviews to tableView cell.
all my custom views are covered by contentView.
adding subviews to contentView should solve the problem.

I also get same this issue, and after some hours debug and find this line is cause:
self.contentView.backgroundColor = [UIColor clearColor];

I just commented it and the issue was fixed! I don't know why it only happen on xcode 12
@dangnv
Does it only occur with buttons in cellView or also with other buttons ?

I have tested with buttons in a view, it works with clear background.
I solved my issue by doing this,

Old - (below iOS 14)

[cell addSubview:__];

New code - (iOS 14)

[cell.contentView addSubview:
__];
How can I fix action button after update Xcode12?
 
 
Q