UIButton addTarget 选择器不起作用

2023-07-07移动开发问题
1

本文介绍了UIButton addTarget 选择器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

SquareBox.swift

SquareBox.swift

class SquareBox {
  func createBoxes() {
    for _ in 0..<xy {
            let button = UIButton()

            button.backgroundColor = .white
            button.setTitleColor(UIColor.black, for: .normal)
            button.layer.borderWidth = 0.5
            button.layer.borderColor = UIColor.black.cgColor
            stack.addArrangedSubview(button)
            button.addTarget(self, action: #selector(click(sender:)) , for: .touchUpInside)
    }
  }

  @objc func click(sender : UIButton) {
    print("Click")
  }
}

ViewController.swift

ViewController.swift

class GameViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let boxRow = SquareBox() 
        boxRow.createBoxes()
    }
}

我也尝试过@IBAction 而不是@objc,它不起作用,但是如果我在创建这个对象的 ViewController.swift 中使用click"函数,它可以工作,但我需要在这个类中使用这个函数.

Also I've tried @IBAction instead of @objc, it doesn't work, but if I use "click" function in ViewController.swift that I created this object, it's working but I need this function inside of this class.

推荐答案

既然您已经在问题中发布了相关信息,那么问题就很清楚了.您遇到了内存管理问题.

Now that you have posted relevant information in your question, the problem is quite clear. You have a memory management issue.

在您的 GameViewController 的 viewDidLoad 中,您创建 SquareBox 的本地实例.此本地实例在 viewDidLoad 结束时超出范围.由于没有其他对该实例的引用,它在 viewDidLoad 结束时被释放.

In your GameViewController's viewDidLoad you create a local instance of SquareBox. This local instance goes out of scope at the end of viewDidLoad. Since there is no other reference to this instance, it gets deallocated at the end of viewDidLoad.

由于 SquareBox 的实例已被释放,它不再充当按钮的目标.而且您的 click 方法永远不会被调用.

Since the instance of SquareBox has been deallocated, it is not around to act as the button's target. And your click method is never called.

解决方案是在视图控制器中保留引用:

The solution is to keep a reference in your view controller:

class GameViewController: UIViewController {
    let boxRow = SquareBox() 

    override func viewDidLoad() {
        super.viewDidLoad()
        boxRow.createBoxes()
    }
}

这篇关于UIButton addTarget 选择器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

[ios.cocos2d+box2d]如何禁用自动旋转?
[ios.cocos2d+box2d]how to disable auto-rotation?([ios.cocos2d+box2d]如何禁用自动旋转?)...
2024-08-12 移动开发问题
7

从 Documents 目录存储和读取文件 iOS 5
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)...
2024-08-12 移动开发问题
9

如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?
How can I implement a virtual joystick for a cocos2d game outside the cocos2d environment?(如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?)...
2024-08-12 移动开发问题
13