简体   繁体   English

如何为以编程方式制作的文本字段创建 IBAction?

[英]How do I create IBAction for a textfield made programmatically?

I've made a UITextField in my view controller.我在我看来 controller 做了一个UITextField

let label = UITextField(frame: CGRect(x: 0, y: 0, width: 300, height: 21))

I was wondering how I would make an IBAction for this.我想知道如何为此制作 IBAction。

I'm making a variable amount of text fields which when edited, I want to take their values and make grade for a class. Each textfields value is an individual assignment and are editable by the user, so when one is edited it recalculates the class grade.我正在制作可变数量的文本字段,在编辑时,我想获取它们的值并为 class 打分。每个文本字段值都是一个单独的分配,可由用户编辑,因此当编辑一个时,它会重新计算 class年级。

You don't need @IBAction , you just create a function that handles the event, and add it as addTarget :您不需要@IBAction ,您只需创建一个 function 来处理事件,并将其添加为addTarget

label.addTarget(self, action: #selector(myTargetFunction), for: <event>)

where myTargetFunction is your function, eg:其中myTargetFunction是您的 function,例如:

@objc func myTargetFunction(_ sender: UITextField) { ...

and <event> is one of theUIControl events available for UITextField . <event>是可用于UITextFieldUIControl 事件之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM