50

I am new to Xcode, working through Swift, so I'm not sure if what I am decribing is actually a bug.

When using interface builder and the assistant editor, I can create lables, buttons etc, and create Outlets and Action in the code with a control-drag.

So long as I am perfect, no problem. But I seem to run into problems if, for example, I make a mispelling of the object name. Or, I choose outlet instead of action. I can't seem to find a way to make a correction that does't seem to totally screw up the IDE. Even if I delete the object in the IB and the code, it seems to leave problems behind when I build. Or, if I try to delete the line in code and re-drag it, a new line of code is created, but the object seems to now reference the new name, and the old, now missing name.

Again, I am working with Swift --- Since I don't use/know Objective-C I don't know if there is a similar behavior using that language.

Appreciate any pointers. (Other than don't use the Interface builder / storyboards / Swift.)

pkamb
  • 33,281
  • 23
  • 160
  • 191
dave_the_dev
  • 1,555
  • 3
  • 15
  • 27
  • TO clarify -- what is the best way to correct a simple misspelling? (Or I just plain picked a bad name, and now desire to fix that -- so the name is not just in the connection, but throughout the code. (i.e., FInd/Replace that includes the IB itself?) – dave_the_dev Jul 02 '14 at 05:30
  • To rename a IBOutlet without deleting the connection try this. http://stackoverflow.com/a/37313505/7250862 – RajeshKumar R Mar 07 '17 at 12:24

3 Answers3

86

Use the Connections inspector to break the connection. Then you can modify/delete the object and/or the code without having to worry.

enter image description here

user3386109
  • 34,287
  • 7
  • 49
  • 68
  • 2
    Thanks -- I updated my own question to clarify: -- what is the best way to correct a simple misspelling (or better yet, to correct to make a variable name more meaningful , that appears multiple time in the code. – dave_the_dev Jul 02 '14 at 05:32
  • 1
    @user1639473 -- There's no find/replace that includes the IB itself (AFAIK, they may add that feature at some point). So the procedure is 1) break the connection 2) find/replace in the code 3) recreate the connection. Step 3 can be done by control-dragging from the IB object to the existing line of code, or you can drag (w/o control) from the empty circle in the *left margin of the code editor*, to the *object in IB*. – user3386109 Jul 02 '14 at 05:50
  • To rename an object use Edit > Refactor > Rename (or right click on an object). This also renames any IBOutlet reference in Interface Builder. – Craig Pickering Oct 17 '15 at 10:11
  • 2
    Craig, you can't use Refactor/Rename when working with Swift. Xcode throws you an error when you try that. – Michiel Overtoom Nov 02 '15 at 10:58
  • 1
    Hi, perhaps is it a little out of subject: I face the same problem (renaming an IBOutlet) in Objective-C. The IBOutlet was created by ctrl-drag'n dropping a button from a storyboard to a .h file. When renaming the IBOutlet in the .h file, the app crashes at the execution in the simulator because of the old IBOutlet name. To fix it, I opened the file .storyboard in a text editor, looked for the XML tag ``, and removed the outdated connection. – pamplemousse_mk2 Aug 30 '16 at 19:41
22

The accepted answer is fine, but there are a few ways to do the same thing.

Method One

Right click the view in the storyboard and then click the little x by the referencing outlet.

enter image description here

Method Two

Right click the view name in the Document Outline. Then click the little x by the referencing outlet.

enter image description here

Method Three

Select the view on the storyboard and then click the Connections Inspector. Then you can click the little x to remove an outlet reference.

enter image description here

Extra References in Code

enter image description here

If you are getting outlet connections in code that you didn't add yourself see this answer.

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
7

You can find unused IBOutlets in the file in which they're declared by looking in the "gutter" of the source editor for an "unfilled hole".

enter image description here

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
Ron Fridman
  • 304
  • 1
  • 6