1

Errors I'm getting:-

I have 81 UITextFields that I want to add another Send Event too. Currently, they are all set to trigger one function for "Did End on Exit" but I want to add "Editing Changed" to all of them in order to call the same function. I know I could draw lines for all 81 TextFields, but I know there is the addTarget function, but I am not sure how it works. TIA.

//My attempt at addTarget
textField.addTarget(self, action: #selector(editedBox(sender:)), for: .editingChanged)

//Creates outlet for boxes
@IBOutlet var textFieldCollection: [UITextField]!

//Action if a box is edited
@IBAction func editedBox(_ sender: AnyObject)  {

}
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49

1 Answers1

-1

here is the method ... textField is your text field .. if you have 81 fields ... you need to loop around and add target to all of them ....

override func viewDidLoad() {
    super.viewDidLoad()
    for textField in textFieldCollection {
        textField.addTarget(self, action: #selector(editedBox(sender:)), for: .editingChanged)
    }




}
@objc func editedBox(sender: AnyObject)  {


}
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49
  • That is still throwing a lot of errors... am I suppose to include it in the button action function or outside? Sorry, very new to swift. – Peter Pryharski May 05 '20 at 00:46
  • add above method in viewDidLoad() – Jawad Ali May 05 '20 at 00:57
  • I included the picture and outlet for the UITextField, but I'm pretty sure that has nothing to do with this – Peter Pryharski May 05 '20 at 01:06
  • let me know if you still face any issue – Jawad Ali May 05 '20 at 01:22
  • I've seen that you ask for upvotes a lot. In [What to do with user asking for upvote and accept?](https://meta.stackoverflow.com/q/297597/1364007) a moderator says "*badgering for votes, bounties and accepts is not what comments are meant for.*". – Wai Ha Lee May 16 '20 at 00:36
  • Great - thanks for listening. I've flagged some of your previous upvote/accept comments for deletion to tidy up a bit. – Wai Ha Lee May 16 '20 at 12:58
  • yeah .. Thank you so much for letting me know ... i use to ask ... if my answer resolve your issue then please accept it ... is it wrong to ask ? – Jawad Ali May 16 '20 at 13:15
  • and if some one accept but do not upVote ... i request him ... to plz appreciate my efforts by upvoting ... is it wrong brother ? – Jawad Ali May 16 '20 at 13:16
  • its knowledge sharing platform ... sometimes we put hours to resolve some one's issue ... we use to chat hours to bring them out of trouble .... in return if they never accept or upvote ... then it hurts ... isn't it brother ? – Jawad Ali May 16 '20 at 13:18
  • Posts about votes and accepts are not useful to the site - if the user is new they will be prompted to accept our upvote an answer that they find useful. It's not necessary to tell them to do this - it's also in [the help center](https://stackoverflow.com/help/someone-answers). There are other questions about this on meta, e.g. [Asking upvote for accepted answer](https://meta.stackexchange.com/q/21258/284827). – Wai Ha Lee May 16 '20 at 13:40
  • If you put in a lot of effort and you don't get an upvote or an accept, that's not the best outcome but others will see the effort you put in and vote accordingly. As you say, this is a knowledge-sharing platform: you're answering not just for the person sealing the question, you're answering for everybody who has the same question and comes across your answer. – Wai Ha Lee May 16 '20 at 13:42
  • 1
    Okay ... got it ... thank you for your concerns ... i will adopt your suggestions – Jawad Ali May 16 '20 at 13:47
  • one more thing ... i usually ask this question ... "does this resolve the issue ? or you need more clarity ?" is it good to ask this ? – Jawad Ali May 16 '20 at 13:54
  • 1
    Look at it the other way. Suppose *you* asked a question. You get an answer but it doesn't help you. You, as the person needing help would then comment on the answer, saying "this doesn't work because x", or "I can't do this because y". Alternatively if it did help you you'd accept the answer. In both cases you didn't need a comment to say "did this help you" (or similar) to prompt you to take action. – Wai Ha Lee May 16 '20 at 14:44