如何在按钮中传递参数
我有一个底部纸,在那张纸上有一个按钮。我希望,当我点击该按钮时,它将转到与之相关的页面。因此,我将其从故事板连接到代码
@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.