Unable to connect IBOutlet from storyboard to UIView subclass

∥☆過路亽.° 提交于 2019-11-26 22:53:27

问题


I am able to right-click and drag from my custom UIView subclass file to the storyboard elements to connect them, but unable to do so the other way around. I believe this is an Xcode bug. Is there anything that I can do to fix this? This is not the first time that it happen already and its annoying.

I already tried cleaning and restarting Xcode several times with no luck.

This does not work (Right clicking from the storyboard to Swift file)

This works (Right clicking from swift file's IBAction to storyboard)

Btw, I am using Xcode 8.1 with Swift 3.0.

UPDATE: I'm not looking for an answer on how to connect IBOutlets/IBActions, because as I mentioned in question above (and also in the screenshots), I am already able to connect them and the app is running fine. The question is more on WHY Xcode wouldn't let me connect from Storyboard > Swift file, but letting me to do so from Swift file > Storyboard. I'm sorry if the question is misleading.


回答1:


We see this issue sometimes too since Xcode 8, and assume this is a Xcode bug.

As a workaround:

  • Write the outlet in code @IBOutlet weak var myUiElement: UIView!

  • Ctrl + Drag from your outlet to the UI element in the Storyboard




回答2:


Its basic, but easy to overlook. Make sure the view controller you are dragging the outlet to is labeled as the custom class you created.




回答3:


Create an outlet like this in Objective-C

@property (nonatomic, weak) IBOutlet UIView *view;

or as in swift 3

@IBOutlet weak var instruction: UILabel?

and then connect it as given in this image.

Sometimes the normal way of connecting action and outlet doesn't work due to subclassing. This way comes handy for such situations.




回答4:


After trying some of the given solution what finally worked is by restarting the my macbook. It seems like a bug on XCode's side. Oh well.




回答5:


Things to check if IBOutlet does not connect:

  1. Is the class defined in storyboard?

    Select the UIViewController in storyboard, then on the right side of the screen, select the identity inspector and assign the appropriate class.

  2. Are the type of outlet you defined match the one on storyboard? For example, we connect UIButton, then the outlet should look like that, write the code for the outlet, then connect to the view controller:

    @IBOutlet weak var theButton: UIButton!
    



回答6:


I've resolved my issue by following process.

  1. Connect your Storyboard view with any other ViewController.
  2. Now, reconnect your desired ViewController with storyboard.

Hope this will work for you!!




回答7:


Another way just create your button name manually in view controller like this -

@IBOutlet weak var button: UIButton!

then connect your button via drag to view controller as image shown [!

PS:- please clean your derived data also.




回答8:


1: Assign the class to viewController.

2: Clean your project with Shift-Alt-Cmd-K.

3: First quit Xcode and clear the derived data. going to: ~/Library/Developer/Xcode/DerivedData




回答9:


press ctrl than drag to swift file and create outlet.



来源:https://stackoverflow.com/questions/40932172/unable-to-connect-iboutlet-from-storyboard-to-uiview-subclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!