ios 无法将 IBOutlet 从故事板连接到 UIView 子类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40932172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to connect IBOutlet from storyboard to UIView subclass
提问by Chan Jing Hong
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.
我可以右键单击并从我的自定义 UIView 子类文件拖动到故事板元素以连接它们,但反过来则无法这样做。我相信这是一个 Xcode 错误。我能做些什么来解决这个问题吗?这已经不是第一次发生了,而且很烦人。
I already tried cleaning and restarting Xcode several times with no luck.
我已经多次尝试清理和重新启动 Xcode,但没有成功。
This does not work (Right clicking from the storyboard to Swift file)
This works (Right clicking from swift file's IBAction to storyboard)
这有效(从 swift 文件的 IBAction 右键单击到情节提要)
Btw, I am using Xcode 8.1 with Swift 3.0.
顺便说一句,我使用 Xcode 8.1 和 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.
更新:我不是在寻找关于如何连接 IBOutlets/IBActions 的答案,因为正如我在上面的问题中提到的(以及在屏幕截图中),我已经能够连接它们并且应用程序运行良好。问题更多是关于为什么 Xcode 不允许我从 Storyboard > Swift 文件进行连接,而是让我从 Swift 文件 > Storyboard 进行连接。如果问题具有误导性,我很抱歉。
采纳答案by Chan Jing Hong
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.
在尝试了一些给定的解决方案之后,最终起作用的是重新启动我的 macbook。这似乎是 XCode 方面的一个错误。那好吧。
回答by shallowThought
We see this issue sometimes too since Xcode 8
, and assume this is a Xcode bug.
我们有时也会看到这个问题,因为Xcode 8
,并假设这是一个 Xcode 错误。
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
用代码编写插座
@IBOutlet weak var myUiElement: UIView!
Ctrl + 从您的插座拖动到故事板中的 UI 元素
回答by ScottyBlades
回答by Nikhil Manapure
Create an outlet like this in Objective-C
在 Objective-C 中创建一个这样的插座
@property (nonatomic, weak) IBOutlet UIView *view;
or as in swift 3
或如 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.
有时,由于子类化,连接 action 和 outlet 的正常方式不起作用。这种方式在这种情况下很方便。
回答by Idan
Things to check if IBOutlet
does not connect:
IBOutlet
未连接时要检查的事项:
Is the class defined in storyboard?
Select the
UIViewController
instoryboard
, then on the right side of the screen, select theidentity inspector
and assign the appropriate class.Are the type of outlet you defined match the one on
storyboard
? For example, we connectUIButton
, then the outlet should look like that, write the code for the outlet, then connect to the view controller:@IBOutlet weak var theButton: UIButton!
类是否在故事板中定义?
选择
UIViewController
instoryboard
,然后在屏幕右侧,选择identity inspector
并分配适当的类。您定义的插座类型与上面的插座类型匹配
storyboard
吗?例如,我们 connectUIButton
,那么插座应该是这样的,编写插座的代码,然后连接到视图控制器:@IBOutlet weak var theButton: UIButton!
回答by Wajahat Hussain
I've resolved my issue by following process.
我已经通过以下过程解决了我的问题。
- Connect your Storyboard view with any other ViewController.
- Now, reconnect your desired ViewController with storyboard.
- 将您的 Storyboard 视图与任何其他 ViewController 连接起来。
- 现在,将您想要的 ViewController 与故事板重新连接。
Hope this will work for you!!
希望这对你有用!!
回答by neha mishra
回答by Mohd Sultan
1: Assign the class to viewController.
1:将类分配给viewController。
2: Clean your project with Shift-Alt-Cmd-K.
2:使用 Shift-Alt-Cmd-K 清理您的项目。
3: First quit Xcode and clear the derived data. going to: ~/Library/Developer/Xcode/DerivedData
3:先退出Xcode,清除导出数据。去: ~/Library/Developer/Xcode/DerivedData
回答by Sagar Snehi
press ctrl than drag to swift file and create outlet.
按 ctrl 而不是拖动到 swift 文件并创建插座。