Swift 如何通过编程识别ViewController子视图中按钮的点击动作?
在我的Swift 如何通过编程识别ViewController子视图中按钮的点击动作?,swift,xcode,Swift,Xcode,在我的mainViewController中有一个视图子视图,子视图有4个按钮。如何识别mymainViewController中这些按钮的onClick操作 下面是我的主视图控制器 import UIKit class HomeViewController: UIViewController{ let tableList = ["HIPAA Rules", "Know Your HIPPA Status", "Covered Entity", "Business Associate"]
mainViewController
中有一个视图子视图
,子视图
有4个按钮。如何识别mymainViewController
中这些按钮的onClick操作
下面是我的主视图控制器
import UIKit
class HomeViewController: UIViewController{
let tableList = ["HIPAA Rules", "Know Your HIPPA Status", "Covered Entity", "Business Associate"]
var metrics : [String : CGFloat] = [:]
let menuBar = MenuBar()
let table = OptionsTable()
var views : [UIView] = []
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.barTintColor = UIColor.getColorFromHex(hexCode: "1B9AF7")
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
setUpTable()
}
private func setUpTable(){
views += [table]
view.addSubview(table)
table.backgroundColor = UIColor.red
//Code For Constraints Of the SubView
table.setUpButtons(numberOfButtons: 4, buttonTitleList: tableList, optionsTableHeight: optionTableHeight)
}
class OptionsTable: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
}
func setUpButtons(numberOfButtons : Int, buttonTitleList : [String], optionsTableHeight : CGFloat) {
let cellHeight = optionsTableHeight / CGFloat(numberOfButtons)
for buttonNumber in 0...numberOfButtons-1{
var views : [UIView] = []
var metrics : [String : CGFloat] = [:]
let paddingInTermsOfButtons = (numberOfButtons-buttonNumber)-1
let button = UIButton()
button.setTitle(buttonTitleList[buttonNumber], for: .normal)
button.backgroundColor = UIColor.white
button.layer.borderColor = UIColor.gray.cgColor
button.layer.borderWidth = 1;
button.setTitleColor(UIColor.black, for: .normal)
print("For button : \(buttonNumber)" )
print(self.frame)
self.addSubview(button)
views += [button]
//Code For Constraints of the buttons
}
}
下面是我的选项表//主ViewController的子视图
import UIKit
class HomeViewController: UIViewController{
let tableList = ["HIPAA Rules", "Know Your HIPPA Status", "Covered Entity", "Business Associate"]
var metrics : [String : CGFloat] = [:]
let menuBar = MenuBar()
let table = OptionsTable()
var views : [UIView] = []
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.barTintColor = UIColor.getColorFromHex(hexCode: "1B9AF7")
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
setUpTable()
}
private func setUpTable(){
views += [table]
view.addSubview(table)
table.backgroundColor = UIColor.red
//Code For Constraints Of the SubView
table.setUpButtons(numberOfButtons: 4, buttonTitleList: tableList, optionsTableHeight: optionTableHeight)
}
class OptionsTable: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
}
func setUpButtons(numberOfButtons : Int, buttonTitleList : [String], optionsTableHeight : CGFloat) {
let cellHeight = optionsTableHeight / CGFloat(numberOfButtons)
for buttonNumber in 0...numberOfButtons-1{
var views : [UIView] = []
var metrics : [String : CGFloat] = [:]
let paddingInTermsOfButtons = (numberOfButtons-buttonNumber)-1
let button = UIButton()
button.setTitle(buttonTitleList[buttonNumber], for: .normal)
button.backgroundColor = UIColor.white
button.layer.borderColor = UIColor.gray.cgColor
button.layer.borderWidth = 1;
button.setTitleColor(UIColor.black, for: .normal)
print("For button : \(buttonNumber)" )
print(self.frame)
self.addSubview(button)
views += [button]
//Code For Constraints of the buttons
}
}
因此,如何识别选项表中这些按钮的onclick操作,因为它们是动态创建的?您必须添加目标才能识别按钮中的单击事件,如下所示 Swift 点击按钮时将调用此方法
@objc func buttonOneClicked(button: UIButton) {
//Do whatever you want to do here and check button tags so you will get to know which index is clicked
}
- (void) buttonOneClicked:(UIButton *) sender {
//Do whatever you want to do here and check button tags so you will get to know which index is clicked
}
目标-C
点击按钮时将调用此方法
@objc func buttonOneClicked(button: UIButton) {
//Do whatever you want to do here and check button tags so you will get to know which index is clicked
}
- (void) buttonOneClicked:(UIButton *) sender {
//Do whatever you want to do here and check button tags so you will get to know which index is clicked
}
为什么不共享
mainViewController
、子视图
等的代码,以便我们更好地了解您的问题。除非您有充分的理由以编程方式执行此操作,否则使用IB和IBAction执行此操作非常简单。@RakeshaShastri请检查我的编辑设置post@claude31,是的,按钮是动态创建的。所以,如果您知道哪个按钮指向哪个viewController,那么应该维护各个变量来保存每个按钮。然后将操作添加到所有按钮。谢谢。我将尝试此操作。如果您有动态按钮,请尝试添加标记。我已经编辑了我的回答谢谢。这很有帮助。