Can't connect IBOutlet in Interface Builder

2019-01-13 11:00发布

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?

19条回答
Bombasti
2楼-- · 2019-01-13 11:14

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...

查看更多
We Are One
3楼-- · 2019-01-13 11:14

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.

查看更多
劳资没心,怎么记你
4楼-- · 2019-01-13 11:21

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!

查看更多
戒情不戒烟
5楼-- · 2019-01-13 11:25

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....

查看更多
爷、活的狠高调
6楼-- · 2019-01-13 11:27

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

查看更多
我想做一个坏孩纸
7楼-- · 2019-01-13 11:30

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.

查看更多
登录 后发表回答