3

I’ve noticed in Swift/Xcode 11 that you can’t ctrl-drag an IB object into a ViewController extension to create an IBAction, but you can cut/paste it into the ext. after ctrl-dragging to create the IBAction in the ViewController class & it works. Anything wrong w/moving an IBAction to a ViewController extension using copy and paste? And why is the Ctrl drag not working for the VC extension? I was putting UITextView actions in an ext w/other TextView funcs ‬when I noticed this but don’t fully understand why the ctrl-drag won’t work but cut/paste appears to show no prob. Thx for considering the Q.

Gallaugher
  • 1,593
  • 16
  • 27

1 Answers1

1

Unless someone works for Apple I'm not sure we'll get an actual answer here. I can guess and give you an alternative that worked for me though.

From an Xcode feature implementation standpoint, the code that supports these actions probably varies..

From what I've seen control drag works when the class of the VC on the storyboard directly matches the class of the object you're dragging into.

I've never tried dragging into extensions but I can see how that might be complicated given that stored properties are not allowed on extensions which adds complications given you can drag outlets too.

However, I've also done IBAction/IBOutlet's on parent classes and I was able to get that to work when I started dragging FROM the an existing IBAction func (there's a circle to the left of it in the code) TO the storyboard of the child class.

In short, I'm assuming there's a lot of assumptions and code being done to allow you to drag from storyboard onto code, and I'm pretty sure limitations are due to the complications in handling the corner cases. Apple will often opt towards not allowing behavior at all if the possibility for a broken corner case exists. Some of the other ways of creating these connections (like a copy and paste) are prob simpler.

gadu
  • 1,816
  • 1
  • 17
  • 31
  • Thx. This is useful. I’ll assume the cut paste of a simple IBAction is ok for organizing UITextField code. Wanted to be sure I wasn’t using (or sharing with my students) a flawed approach. – Gallaugher Feb 20 '20 at 18:28
  • out of curiosity why do you even need to do this. typically if its annoying or complicated to do there's a better solution doing it some other way – gadu Feb 20 '20 at 18:49
  • I've never really used IBAction's for UITextField (so not positive what you're using it for) but I've always just done `textField.delegate = self` and implemented the UITextFieldDelegate methods I care about. – gadu Feb 20 '20 at 18:55
  • Thx. I use it for input validation. I’m using textFieldEditingDidChange and checking to see if .text > 0. If it is I enable a Save bar button, otherwise I disable the button so a blank value isn’t created. – Gallaugher Feb 20 '20 at 20:21
  • yeah just use the delegate instead, keeps things much simpler only in code: https://developer.apple.com/documentation/uikit/uitextfielddelegate – gadu Feb 24 '20 at 16:15