简体   繁体   English

Xcode中的Control-drag给出了出口,但是UIButton没有动作。 为什么?

[英]Control-drag in Xcode gives outlets, but no action for UIButton. Why?

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?). 在我正在构建的应用程序中,我有一个导航控制器,其中一个View Controller作为子视图连接(这是正确的词吗?)。 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。

Screenshot of what I'm seeing 我所看到的屏幕截图

It's a pretty simple app, and I've tried removing my Pods as a fix, but so far no luck. 这是一个非常简单的应用程序,我曾尝试删除Pods作为修复程序,但到目前为止还没有运气。

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. 我有一个带有UINavigationItem的导航控制器,其中显示了一个子ViewController。 The button in question is in the sub ViewController and I setup a new swift file to handle that ViewController's class. 有问题的按钮在子ViewController中,我设置了一个新的swift文件来处理该ViewController的类。 I'm trying to connect that button into that class file, and that's where it's not working. 我正在尝试将该按钮连接到该类文件中,这是它不起作用的地方。

Screenshot of my storyboard 我的故事板的屏幕截图

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.

    }

}

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文件中创建一个动作。

也许您尝试连接到其他ViewController,请再次检查它是否正确并正确连接到Storyboard上

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. 您的代码已经完成了。

这是我的意思的一个例子

控制拖动后,它看起来像这样

选择视图控制器,然后分配类

当我在项目中添加本地化时,也发生了这种情况。从那时开始,在本地化中添加了情节提要,情节提要按钮不显示任何操作选项,仅显示插座选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法控制将UIButton拖动到IBOutletCollection - can't control-drag UIButton to IBOutletCollection 控制 - 拖动以在Xcode 5中创建IBAction以关闭键盘? - Control-drag to create IBAction in Xcode 5 to dismiss keyboard? UIButton 的 NSUnknownKeyException。 正确添加引用出口及对应功能 - NSUnknownKeyException for a UIButton. Correctly added referencing outlets and corresponding function IOS - 控制 - 拖动按钮不起作用 - IOS - control-drag of a button is not working 在 Storyboard 中按住 Control 并拖动 UIView 以显示 controller? - Control-drag UIView in Storyboard to present a controller? 无法控制在故事板中的可可触摸uiview类和子视图之间的xcode中拖动 - Cannot control-drag in xcode between cocoa touch uiview class and subview in storyboard 无法控制拖动到ViewController.swift - Cannot Control-Drag into ViewController.swift UIBarButtonType作为UIButton。 不在导航栏上! - UIBarButtonType as a UIButton. Not on a NavigationBar! iOS操作:将UIButton添加到另一个UIButton时出现@selector问题。 动作仅适用于按钮相交的位置 - iOS action:@selector issue when UIButton is added to another UIButton. Action only works where buttons intersect 为什么我需要控制某种UI小部件并将其拖动到代表视图控制器的黄色区域中? - Why do I need to control-drag some kind of UI-widgets into yellow sphere representing the view controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM