Swift:以编程方式在 UICollectionViewCell 中的 IndexPath 中的多个 UIButton,下一个 addTarget 不单击

Swift: Multiple UIButton in a IndexPath in a UICollectionViewCell programmatically, next addTarget does not click(Swift:以编程方式在 UICollectionViewCell 中的 IndexPath 中的多个 UIButton,下一个 addTarget 不单击)
本文介绍了Swift:以编程方式在 UICollectionViewCell 中的 IndexPath 中的多个 UIButton,下一个 addTarget 不单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以编程方式在 UICollectionViewCell 的 indexPath 中创建了多个 UIButton.现在我想打印单击添加到收藏夹",但单击 UIButton 后它没有打印任何内容.UIButton 确实可以查看除 addTarget 功能之外的所有内容,但未单击.

I made multiple UIButton in a indexPath in a UICollectionViewCell programmatically. now i want to print "clicked add to favourite" but it is not printing anything after click UIButton. the UIButton does view everything except addTarget function not click.

import UIKit

//**UICollectionViewCell**
class DescriptionCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {


    override init(frame: CGRect) {
        super.init(frame: frame)


        setupCell()

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }



    lazy var addToCart: UIButton = {

        let btn = UIButton()
        btn.setTitle("Add to favourite", for: .normal)
        btn.isUserInteractionEnabled = true
        btn.setTitleColor(.white, for: .normal)

        btn.backgroundColor = UIColor.orange
        btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 8)
        btn.titleLabel?.textAlignment = .right

        // addToCart not click
        btn.addTarget(self, action: #selector(addToCartTarget), for: .touchUpInside)

        btn.layer.cornerRadius = 12

        return btn
    }()




    func setupCell() {



        addSubview(addToCart)



        addConstraintsWithFormat("H:|-80-[v0]-80-|", views: addToCart)
             addConstraintsWithFormat("V:|-380-[v0(25)]|", views: addToCart)


    }



    func addToCartTarget() {

        print("you clicked add to favourite")
    }




}

推荐答案

尝试用如下框架初始化按钮:

Try initializing button with frame like:

let btn = UIButton(frame: CGRect(x: 10, y: 10, width: 50, height: 20))

这篇关于Swift:以编程方式在 UICollectionViewCell 中的 IndexPath 中的多个 UIButton,下一个 addTarget 不单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)