【问题标题】:Can't connect custom IBAction to button无法将自定义 IBAction 连接到按钮
【发布时间】:2016-02-16 22:28:56
【问题描述】:

我正在学习 swift (http://www.raywenderlich.com/104575/researchkit-tutorial-with-swift) 中的 ResearchKit 教程,但我无法将任何 IBaction 方法连接到主情节提要中的按钮。这是代码(来自教程):

import ResearchKit

 class ViewController: UIViewController {

    @IBAction func consentTapped(sender : AnyObject) {
        let taskViewController = ORKTaskViewController(task: ConsentTask, taskRunUUID: nil)
        taskViewController.delegate = self
        presentViewController(taskViewController, animated: true, completion: nil)
    }

    @IBAction func surveyTapped(sender : AnyObject) {
        let taskViewController = ORKTaskViewController(task: SurveyTask, taskRunUUID: nil)
        taskViewController.delegate = self
        presentViewController(taskViewController, animated: true, completion: nil)
    }
}

extension ViewController : ORKTaskViewControllerDelegate {

    func taskViewController(taskViewController: ORKTaskViewController, didFinishWithReason reason: ORKTaskViewControllerFinishReason, error: NSError?) {
        taskViewController.dismissViewControllerAnimated(true, completion: nil)
      }

}

我进入主情节提要并单击 View Controller,我尝试将我的类设置为 ViewController 和 UIViewController,并且这些方法应该显示在 Sent Events 中,但它们没有。我也尝试过 ctrl 拖动,但也没有用。

【问题讨论】:

  • 我认为你需要一个强引用来持有 taskViewController 来呈现。

标签: ios xcode swift ibaction researchkit


【解决方案1】:

问题是当视图控制器类设置为我的 IBaction 方法所在的类时,我没有从按钮直接拖动到黄色视图控制器按钮。

【讨论】:

  • 我建议您将自己的答案标记为已接受,因此它不会显示在“未接受答案的问题”列表中。
【解决方案2】:

您可以手动将@IBAction 的参数从(_ sender: Any) 更改为(_ sender: AnyObject)。 请尝试使用 Ctrl+Drag。

它可能会起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多