Xcode 中的 Control-drag 提供了出口,但没有 UIButton 的操作。为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43480409/
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
Control-drag in Xcode gives outlets, but no action for UIButton. Why?
提问by Charles Forster
In my app I'm building, I've got a Navigation Controller with a View Controller connected as a subview (is that the right word?). I added a UIButton and tried to Control-drag it into my view controller class file, but it's only giving me Outlet and Outlet collection as my options, not Action.
在我正在构建的应用程序中,我有一个带有作为子视图连接的视图控制器的导航控制器(这是正确的词吗?)。我添加了一个 UIButton 并尝试将它控制拖动到我的视图控制器类文件中,但它只给我 Outlet 和 Outlet 集合作为我的选项,而不是 Action。
It's a pretty simple app, and I've tried removing my Pods as a fix, but so far no luck.
这是一个非常简单的应用程序,我已经尝试删除我的 Pod 作为修复程序,但到目前为止还没有运气。
I've also tried manually writing the function and trying to connect it back to the button, but to no avail. Dragging back to the button in the workspace doesn't work, and the function isn't firing when I test the app. Here's the code I tried:
我还尝试手动编写函数并尝试将其连接回按钮,但无济于事。拖回到工作区中的按钮不起作用,并且在我测试应用程序时该功能没有触发。这是我试过的代码:
@IBAction func tapSaveButton(_ sender: UIButton) {}
It's probably something simple, so what am I missing here?
这可能很简单,那么我在这里错过了什么?
EDIT: Also, here's how I'm set up. I have a Navigation Controller with a UINavigationItem that shows a sub ViewController. The button in question is in the sub ViewController and I setup a new swift file to handle that ViewController's class. I'm trying to connect that button into that class file, and that's where it's not working.
编辑:另外,这是我的设置方式。我有一个带有显示子 ViewController 的 UINavigationItem 的导航控制器。有问题的按钮位于子 ViewController 中,我设置了一个新的 swift 文件来处理该 ViewController 的类。我正在尝试将该按钮连接到该类文件中,但这就是它不起作用的地方。
Here's my ViewController class's code:
这是我的 ViewController 类的代码:
import Foundation
import UIKit
import KeychainSwift
class SettingsViewController : UIViewController {
let keychain = KeychainSwift()
@IBOutlet weak var APIKeyField: UITextField!
@IBOutlet weak var WebSocketIPField: UITextField!
@IBOutlet weak var SettingsSaveButton: UIButton!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
/* My other code, not related */
}
@IBAction func tapSaveButton(_ sender: UIButton) {
// My button actions here.
}
}
采纳答案by Charles Forster
Of course it was something dumb. I removed the class from the button, and viola, it let me create an action in my Swift file.
当然,这是愚蠢的事情。我从按钮和中提琴中删除了这个类,它让我在我的 Swift 文件中创建一个动作。
回答by Felix Cruz
maybe you tried to connect to other viewcontroller please check again it is correct viewcontroller and connected on storyboard exactly
也许您尝试连接到其他视图控制器,请再次检查它是正确的视图控制器,并完全连接到故事板上
回答by Kratos
It's also happen with me, when i added localization in my project, from that time which storyboard was added in localization, that storyboard buttons are not showing any action option, only showing outlet options.
这也发生在我身上,当我在我的项目中添加本地化时,从那时起在本地化中添加了哪个情节提要,情节提要按钮不显示任何操作选项,只显示出口选项。
回答by Meshach
Right click on the uibutton within storyboard, you should see a list of action options under "Sent Events" the most common one to use is "Touch Up Inside".
右键单击情节提要中的 uibutton,您应该会在“已发送事件”下看到一系列操作选项,最常用的是“Touch Up Inside”。
Drag from the one you want (circle to the side) onto your view controller and everything should work fine.
从你想要的(圈到一边)拖到你的视图控制器上,一切都应该正常工作。
EDIT: For clarity, you can even drag from "Touch Up Inside" to the func you've already created so long as it's labeled
编辑:为了清楚起见,您甚至可以从“Touch Up Inside”拖动到您已经创建的功能,只要它被标记
@IBAction ...
which your code already has done.
您的代码已经完成了。