如何在按钮中传递参数

发布于 2025-02-12 16:03:37 字数 1526 浏览 3 评论 0原文

我有一个底部纸,在那张纸上有一个按钮。我希望,当我点击该按钮时,它将转到与之相关的页面。因此,我将其从故事板连接到代码

@IBOutlet weak var openPage: SheetOpenPageButton!

,并为按钮编写扩展名。

class SheetOpenPageButton: UIButton {
    var navigationController: UINavigationController?
    override var isSelected: Bool {
        didSet {
            if isSelected {
               self.backgroundColor = UIColor(named: "CustomRed")
            } else {
                self.backgroundColor = UIColor(named: "CustomGray")
            }
        }
    }
    
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!

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

在我要单击按钮的CollectionView中,我调用这些方法。

let button = SheetOpenPageButton()
                button.tag = cell.id ?? 1
                button.addTarget(self, action: #selector(goToRestuarantFromStory(_ :)), for: .touchUpInside)

  @objc func goToRestuarantFromStory(_ sender : UIButton) {
        let restuarantVC = UIStoryboard(name: "Cafe", bundle: .main).instantiateViewController(withIdentifier: "RestuarantViewController") as! RestuarantViewController
        restuarantVC.networkId = sender.tag
        navigationController?.pushViewController(restuarantVC, animated: true)
        print("smth")
        removeSubview()
        removeSubviewSpb()
        vc.dismiss(animated: true, completion: nil)
    }

在这里,我使用按钮的标签在addTarget中传递参数。 但是,当按下按钮时,它不会产生反应。我不知道该如何解决。

I have a bottom sheet, and on that sheet there is a button. I want that when i tap to that button it go to the page it is related to. So, i connected that from storyboard to code

@IBOutlet weak var openPage: SheetOpenPageButton!

And wrote extension for the button.

class SheetOpenPageButton: UIButton {
    var navigationController: UINavigationController?
    override var isSelected: Bool {
        didSet {
            if isSelected {
               self.backgroundColor = UIColor(named: "CustomRed")
            } else {
                self.backgroundColor = UIColor(named: "CustomGray")
            }
        }
    }
    
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!

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

In the collectionview, where i am clicking on button, i call these methods.

let button = SheetOpenPageButton()
                button.tag = cell.id ?? 1
                button.addTarget(self, action: #selector(goToRestuarantFromStory(_ :)), for: .touchUpInside)

  @objc func goToRestuarantFromStory(_ sender : UIButton) {
        let restuarantVC = UIStoryboard(name: "Cafe", bundle: .main).instantiateViewController(withIdentifier: "RestuarantViewController") as! RestuarantViewController
        restuarantVC.networkId = sender.tag
        navigationController?.pushViewController(restuarantVC, animated: true)
        print("smth")
        removeSubview()
        removeSubviewSpb()
        vc.dismiss(animated: true, completion: nil)
    }

Here i am passing parameter in addtarget using button's tag.
But, when tapping on button it doesn't give a reaction. I don't know how to solve this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪我们做朋友吧 2025-02-19 16:03:37

SheetOpenPageButton不需要导航controller,它既不使用也不需要在此处显示的代码中实例化。

您在gotoreStuarantFromStory [sic]中的navigationContoller来自收集范围的范围,并且根据您的代码也不是零,因此pushViewController从未实际调用过。

SheetOpenPageButton does not need a navigationController, it's neither used nor instantiated in the code you have shown here.

The navigationContoller your refer to in goToRestuarantFromStory[sic] is from the scope of your collectionView and according to your code also nil, so pushViewController is never actually called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文