Can't connect IBOutlet in Interface Builder

2019-01-13 10:54发布

问题:

I'm having a weird experience. I create any type of iPhone application, add a UIViewController with Xib file. I can edit the xib file with controls and see them rendered if I run. Now i'm trying to add a few IBOutlets to the controller, so I add them to the .h file, synthesize on the .m file. Then i head over to Interface Builder to connect the outlets. I drag the UILabel to the UIView, and then i try to connect the File Owner (which my custom UIViewController), but all that i can select when i try to connect to the UILabel is "view", which is obviosly wrong. So I'm not able to connect IBOutlets to their controls in Interface Builder.

Any ideas?

回答1:

I did a combination of the things in this thread and I finally had success.

In Interface builder (not xcode)

  1. File->Reload All Class Files
  2. File->Read Class Files (select MyClass.h)
  3. Reconnect File's Owner by
    a. Setting the Class to "MyClass"
    b. Reconnecting the View to the File's Owner's View

Everything is back to normal now. Weird.

Hope this helps more than it confuses ;-)



回答2:

In Interface Builder File->Read Class Files, and point it to your File's Owner's class. That fixed it for me.



回答3:

Had the same problem.

Fixed it by double checking my File's Owner "Class" attribute on the Identity inspector.



回答4:

Are you sure the File's Owner is correctly set to your custom UIViewController and that you've saved your .h file after having added the IBOutlet keyword to the instance variable and property?



回答5:

I had pretty much the same problem as you did. I don't know if you solved your problem yet but mine was that I changed all the classes' names, including the viewcontroller, but the file's owner's name remain unchanged. So, I double clicked File's owner -> click i in the window opened -> change the name in the Class field to the new viewcontroller name. Then it worked. Maybe you want to try it again and see if it works. Good luck.



回答6:

Did you make sure you are defining your outlets in your .h file as the appropriate type? If you are trying to connect a text field in IB, then your .h file should look something like...

IBOutlet UITextField    *MyTextField;

Make sure the type is UITextField (or UITextView, etc). If you type them as NSString or anything the is not appropriate, then you can't connect them in Interface Builder....



回答7:

If you failed to connect your UI controls to code via Right-click (or CTRL) -> dropped down list, there is another way. In interface builder, press & hold CTRL, drug mouse from "File's Owner" in the Main window of Interface Builder to your UI control (label, button, etc). The dpopdown list with all defined by you Outlets will be appered. Just choice needed one!



回答8:

I had the same problem for the LONGEST. Changing my File's Owner "Type" to "MyClassViewController" instead of "UIViewController" in the Class identity window fixed my issue. It makes sense because as the File's owner type is UIViewController it's only going to show those properties specific to the base class. Changing the type to MyClassViewController gives access to it's outlets and all inherited instances. Hope this helps!



回答9:

File->Read Class Files (select MyClass.h) Did the job for me.



回答10:

XCode -> Product -> Clean

Worked for me.



回答11:

I had the same problem, and I just tried diatrevolo's suggestion: Click File, then Read Class Files, and point to your File's s Owner class.

This fixed it for me. I would +1 diatrevolo, but I don't have any reputation yet...



回答12:

kills me every time, but sometimes if you just close and reopen xcode, things are fixed... Just happened to me with this exact problem...



回答13:

For me the issue was that I had created a MyUITextView extending the UITextView and X-Code would'nt connect. My workaround was to use UITextView in IBOutlet temporarily to connect/link and then change to my custom view.

So I changed.

IBOutlet MyUITextView *tv;

to

IBOutlet UITextView *tv;

and then connected/ linked. Once connected, I changed my OBOutlet back to MyUITextView

IBOutlet MyUITextView *tv;


回答14:

Restarting Xcode 5.1 did it for me.



回答15:

So I don't know exactly why it doesn't automatically make the IBOutlet connections, but i found a workaround. I had to manually add the outlets through the Library in Interface Builder for my customer view controller. If anyone finds the fix to make it automatically read from the .h file (like its supposed to), I'd be very appreciative if they posted it.

thank you



回答16:

I'm a total newbie at this, and I had the same problem. I went to try diatrevolo's suggestion, and, in doing so, found out what was causing my problem. I had originally created my ViewController somewhere random in the project hierarchy, and then moved them into the classes folder. This meant that the actual files were not in the classes folder, but the project thought that they were, so IB was looking in the wrong place. I moved the actual files in to the actual classes file with Finder, and then deleted and readded them in the project. Then everything worked!



回答17:

After I localized some XIB files, the views did not react to any updates made in Interface Builder. Simple solution was to delete the app from the simulator / device. The next Build&Run updated the application correctly.

This only happened while deploying the app via XCode on the simulator. Seems like XCode is trying to optimize the build and gets confused.



回答18:

I was having trouble here too, and what ended up working for me was to build the app prior to opening Interface Builder. Voodoo!



回答19:

I wrote out

@IBOutlet weak var title: UILabel!

in the UIViewController text file then connected from the file to the label in the storyboard, and it worked!