无法将按钮操作拖动到 Xcode 8 中的现有功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39606073/
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
Can't drag button action to existing function in Xcode 8
提问by Jaros?aw Krajewski
I have a weird issue with Xcode 8. When I try to ctrl + drag button to existing function, I can't I can only create new outlet or Action. What is more weird, that even when I create action by ctrl + drag i can't connect event the same button that I used for creating action. IB icon is also blank like there is no connection between action and button, but real time clicking runs action.
On the other hand when I try to connect action to button from swift to storyboard I can (@IB icon becomes "full"), but then I get exception on real time clicking unrecognized selector myActionWithSender
我对 Xcode 8 有一个奇怪的问题。当我尝试按 ctrl + 拖动按钮到现有功能时,我不能只创建新的插座或操作。更奇怪的是,即使我通过 ctrl + 拖动创建动作,我也无法将事件连接到我用于创建动作的相同按钮。IB图标也是空白的,就像动作和按钮之间没有联系,但实时点击运行动作。另一方面,当我尝试将按钮从 swift 连接到故事板时,我可以(@IB 图标变为“完整”),但是在实时单击无法识别的选择器 myActionWithSender 时出现异常
采纳答案by Jaros?aw Krajewski
To fix it, I had to update my project language to Swift 3.0 from Swift 2.3
为了修复它,我不得不将我的项目语言从 Swift 2.3 更新为 Swift 3.0
回答by Umut Uygar
In my Xcode version, IBAction is created automatically with "Any" parameter, instead of "AnyObject". Creating IBAction explicitly with UIButton instead worked for me.
在我的 Xcode 版本中,IBAction 是使用“Any”参数自动创建的,而不是“AnyObject”。使用 UIButton 显式创建 IBAction 对我有用。
回答by Didats Triadi
In my case, you will need to change Any to UIButton. It need to be specific that the action coming from UIButton.
就我而言,您需要将 Any 更改为 UIButton。需要具体说明来自 UIButton 的操作。
@IBAction func buttonClicked(_ sender: UIButton) {
}
回答by Alaa Qaddoumi
Click on the UIButton, then go to "Connections Inspector" from Utilities area.
单击 UIButton,然后从 Utilities 区域转到“Connections Inspector”。
Click on the empty circle beside the event and drag the line into func body.
单击事件旁边的空圆圈并将该行拖到 func body 中。
回答by ronak patel
right click with open button function in select touchup inside to drag ..u should try this..
右键单击打开按钮功能,在里面选择 touchup 拖动..你应该试试这个..
回答by Thanh Luu
Change sender from Any to AnyObject works for me
将发件人从 Any 更改为 AnyObject 对我有用
回答by Lin
Even in Swift 3, you'd like to change the @IBAction
's argument from (_ sender: Any)
to (_ sender: UIButton)
or (_ sender: AnyObject)
, but manually.
即使在 Swift 3 中,您也希望将@IBAction
's 参数从更改(_ sender: Any)
为(_ sender: UIButton)
or ( _ sender: AnyObject)
,但需要手动更改。
It might work.
它可能会起作用。
回答by Hem
In my case, I was had a similar storyboard open. Once I closed it and then - clicked the button once to see the blocks around it for alignment. - Then right clicked and dragged it to the ViewController. - Lastly, click/tap where you want to place the event's code block inside the ViewController.
就我而言,我打开了一个类似的故事板。一旦我关闭它然后 - 单击按钮一次以查看它周围的块以进行对齐。- 然后右键单击并将其拖动到 ViewController。- 最后,单击/点击要在 ViewController 中放置事件代码块的位置。
回答by briggsm
In your .swift file change:
在您的 .swift 文件更改中:
@IBAction func buttonClicked(sender: UIButton) {
// Do something
}
@IBAction func buttonClicked(sender: UIButton) {
// Do something
}
to
到
@IBAction func buttonClicked(z sender: UIButton) {
// Do something
}
@IBAction func buttonClicked(z sender: UIButton) {
// Do something
}
Just add that little 'z' (or whatever label you want really). I tried an underscore '_', and that works as well, but it gives a warning, so I just used a 'z' instead to get around the warning.
只需添加那个小“z”(或您真正想要的任何标签)。我尝试了下划线“_”,它也有效,但它给出了警告,所以我只是使用了“z”来绕过警告。
I believe it's a bug in Xcode 8. Somewhere (IB probably) it's expecting Swift 3 compliant code where the first parameter MUST have a label. So when it sees the word "sender" it assumes (wrongly) that that's the label. So adding a label ('z' in my case) makes everybody happy.
我相信这是 Xcode 8 中的一个错误。在某个地方(可能是 IB),它期待 Swift 3 兼容代码,其中第一个参数必须有一个标签。因此,当它看到“发件人”这个词时,它会(错误地)认为那是标签。所以添加一个标签(在我的例子中是'z')会让每个人都开心。
Swift 2.3 does not require a label. Swift 3 does. So, as Jaroslaw pointed out upgrading from 2.3 to 3.0 will also get around this problem. But if you're project is stuck on 2.3, just adding a label seems to do the trick.
Swift 2.3 不需要标签。Swift 3 确实如此。因此,正如 Jaroslaw 指出的那样,从 2.3 升级到 3.0 也将解决这个问题。但是,如果您的项目停留在 2.3 上,只需添加一个标签似乎就可以解决问题。