【问题标题】:IBAction func on button not triggering按钮上的 IBAction 函数未触发
【发布时间】:2016-05-18 22:46:19
【问题描述】:

我在 VC 中有 3 个按钮,都连接到 IBAction 函数。其中两个工作正常,但提交按钮根本不会触发。

我已确保已启用用户交互。我也尝试添加 sender: AnyObject 作为参数并将函数重新连接到按钮,但仍然没有运气。我也清理了项目。我对发生的事情感到非常困惑。

VC 的外观如下:

连接按钮:

按钮的可访问性:

这是每个 IBAction 函数的代码:

@IBAction func captureImage(){
    self.saveVideoVar = false
    let imageFromSource = UIImagePickerController()
    imageFromSource.delegate = self
    imageFromSource.allowsEditing = false 

    //if there is a camera
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
        imageFromSource.sourceType = UIImagePickerControllerSourceType.Camera
        self.presentViewController(imageFromSource, animated: true){}
    }
    else{
        let title = "Error"
        let message = "Could not load camera"

        let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil))
        presentViewController(alert, animated: true, completion: nil)
    }

}

@IBAction func openImageLibrary(){
    self.saveVideoVar = false
    let imageFromSource = UIImagePickerController()
    imageFromSource.delegate = self
    imageFromSource.allowsEditing = false

    imageFromSource.sourceType = UIImagePickerControllerSourceType.PhotoLibrary

    //presents (loads) the library
    self.presentViewController(imageFromSource, animated: true){}
}

//code to submit image and video to amazon S3
@IBAction func submitToS3(){

    print("x")


    if let img : UIImage = imageView.image! as UIImage{
        let path = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("image.png")
        let imageData: NSData = UIImagePNGRepresentation(img)!
        imageData.writeToFile(path as String, atomically: true)

        // once the image is saved we can use the path to create a local fileurl
        let url:NSURL = NSURL(fileURLWithPath: path as String)
        nwyt.uploadS3(url)

    }

}

点击提交按钮的控制截图:

天啊!我觉得我好笨。有一个重复的屏幕我忘记删除了,它看起来完全一样,但不是正在显示的那个。我要在一个小时内删除它。以下是问题:

【问题讨论】:

  • 我怀疑提交按钮不可触摸..你能检查一下..
  • 您的意思是在按钮的“辅助功能”部分吗?我更新了我的帖子显示启用了用户交互

标签: ios swift function uibutton ibaction


【解决方案1】:

通过为按钮设置背景颜色进行检查,以便您了解按钮上方是否有任何视图。

【讨论】:

  • 我为工作按钮和不工作按钮添加了背景颜色。它们出现在 Storyboard 视图中,但是当我运行应用程序时,我看不到背景颜色。但是,无论如何,其中两个按钮仍然可以使用...
  • 这意味着一些其他视图,即动态视图位于按钮上方,因此您无法单击它们。
  • 呃,我是个白痴。我在原始问题中添加了问题所在。
【解决方案2】:

试试这个:

//code to submit image and video to amazon S3
@IBAction func submitToS3(sender:UIButton){

print("x")

if let img : UIImage = imageView.image! as UIImage{
    let path = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("image.png")
    let imageData: NSData = UIImagePNGRepresentation(img)!
    imageData.writeToFile(path as String, atomically: true)

    // once the image is saved we can use the path to create a local fileurl
    let url:NSURL = NSURL(fileURLWithPath: path as String)
    nwyt.uploadS3(url)
}

}

似乎缺少回调参数。应该管用。手指交叉!!

请在属性检查器中检查已启用的属性!

检查按钮名称!

创建一个新按钮和新方法。尝试并钩住。我曾经遇到过这样的问题。如果您使用的是 xCode 7,则可能是 xCode 问题。

【讨论】:

  • 感谢您的回复,但我只是补充说我已经尝试过以及重新连接该功能,但可惜没有成功。
  • 我用按钮的辅助功能信息更新了我的帖子,你是这个意思吗?
  • 正确。等一下!
  • 在哪里查看按钮名称?我不知道它有一个
【解决方案3】:

我可以看到“submitToS3:”中有一个额外的“:”,意思是函数 submitToS3 应该有一个参数,这不是你的情况。

为了解决这个问题,只需删除 submitToS3 链接,然后从提交按钮拖放到控制器上方的黄色图标,并将其链接到“submitToS3”(你应该在那里看到它)。回看 Received Actions 视图时,您应该看不到“:”

【讨论】:

  • 啊,这是因为我在实际代码中将 (sender: AnyObject) 作为函数参数,但在 SO 上发布时将其删除。无论如何,我再次尝试不使用(发件人:AnyObject),重新连接它并显示冒号已被删除但它仍然不起作用:(
  • 嗯,我猜你已经尝试删除提交按钮,删除整个功能,然后重做整个事情?我通常喜欢通过从左侧或右侧的简单拖放来与助理编辑器进行链接,它对我来说总是很有吸引力。能否请您右键单击“提交”按钮并将您看到的内容截图?
  • 您可以尝试这样做吗:创建一个名为 submitToAmazonS3 的新函数(或不同于 submitToS3 的其他函数),然后将您的按钮链接到该新函数。它应该像这样工作。就像现在的解决方法一样......
  • 我试过了,所以现在有两个 IBAction 函数连接到它......仍然无法正常工作:|
  • 是的,去掉submitToS3的旧连接,这样你就只有submitToAmazonS3的连接了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多