Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么';t我的addTarget';按钮工作时的功能?_Ios_Swift - Fatal编程技术网

Ios 为什么';t我的addTarget';按钮工作时的功能?

Ios 为什么';t我的addTarget';按钮工作时的功能?,ios,swift,Ios,Swift,我正在实现一些函数,一旦点击一个按钮就会被调用,但是函数的主体不会被调用。 下面是我的代码,它演示了我需要做什么 let editProfileFollowButton: UIButton = { let button = UIButton(type: .system) button.setTitle("Edit Profile", for: .normal) button.setTitleColor(.black, for: .normal)

我正在实现一些函数,一旦点击一个按钮就会被调用,但是函数的主体不会被调用。 下面是我的代码,它演示了我需要做什么

let editProfileFollowButton: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("Edit Profile", for: .normal)
        button.setTitleColor(.black, for: .normal)
        button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
        button.layer.borderColor = UIColor.lightGray.cgColor
        button.layer.borderWidth = 1
        button.layer.cornerRadius = 3
        button.addTarget(self, action: #selector(handleEditProfileOrFollow), for: .touchUpInside)
        return button
    }()
    @objc func handleEditProfileOrFollow () {
        print("Execute edit profile or follow ")
    }
这就是我的初衷

addSubview(editProfileFollowButton)
        editProfileFollowButton.setAnchor(top: postsLabel.bottomAnchor, left: postsLabel.leftAnchor, right: followingLabel.rightAnchor, bottom: nil, paddingBottom: 0, paddingLeft: 0, paddingRight: 0, paddingTop: 2, height: 34, width: 0)
    }

PS:setAnchor是我创建的一个函数,用于以编程方式设置视图的约束。根据您显示的代码,最明显的是使用了
let

当使用闭包样式用操作构建变量时,我一直认为应该使用
惰性var
实例化。我相信这是因为编译时不知道
self

lazy var editProfileFollowButton: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("Edit Profile", for: .normal)
        button.setTitleColor(.black, for: .normal)
        button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
        button.layer.borderColor = UIColor.lightGray.cgColor
        button.layer.borderWidth = 1
        button.layer.cornerRadius = 3
        button.addTarget(self, action: #selector(handleEditProfileOrFollow), for: .touchUpInside)
        return button
    }()
    @objc func handleEditProfileOrFollow () {
        print("Execute edit profile or follow ")
    }

lazy var
的使用是一个属性,其初始值直到第一次使用时才计算出来。()

从您展示的代码中,最明显的是您使用了
let

当使用闭包样式用操作构建变量时,我一直认为应该使用
惰性var
实例化。我相信这是因为编译时不知道
self

lazy var editProfileFollowButton: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("Edit Profile", for: .normal)
        button.setTitleColor(.black, for: .normal)
        button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
        button.layer.borderColor = UIColor.lightGray.cgColor
        button.layer.borderWidth = 1
        button.layer.cornerRadius = 3
        button.addTarget(self, action: #selector(handleEditProfileOrFollow), for: .touchUpInside)
        return button
    }()
    @objc func handleEditProfileOrFollow () {
        print("Execute edit profile or follow ")
    }

lazy var
的使用是一个属性,其初始值直到第一次使用时才计算出来。()

能否显示将按钮添加到视图层次结构的代码。addTarget不会保留接收器。你确定赛尔夫没有被解除分配吗?@Werneraltwisher你能解释一下吗?这是内存问题吗?能否显示将按钮添加到视图层次结构的代码。addTarget不会保留接收器。你确定赛尔夫没有被解除分配吗?@Werneraltwisher你能解释一下吗?这是一个记忆问题吗?非常感谢,这对我来说确实有效,但我仍然不明白为什么使用lazy var可以解决这个问题?我知道什么是lazy-var以及何时使用它,但在这个场景中使用它与我的想法相去甚远,因为我看到在这种情况下它没有任何用处。我认为这与在闭包中向按钮添加一个自我作为目标有关。如果您是在实例化之后完成的,则在init调用中。我想可以。我对collectionView/tableView也这样做。如果成功了,你能把答案标记为正确吗:PI保证一旦我获得了这样做的声誉,我会投票给你的答案:D解释发生了什么:你用来定义
let
值的函数根本没有访问类的权限(在本例中是ViewController)。该函数中的
self
不是VC,请尝试在其中添加
print(“self:\(String(description:self)))”
。原因是:现在还没有VC。当你把它变成一个懒惰的var时,有一个VC,self引用了它。非常感谢你,这对我来说确实有效,但我仍然不明白为什么使用懒惰的var可以解决这个问题?我知道什么是lazy-var以及何时使用它,但在这个场景中使用它与我的想法相去甚远,因为我看到在这种情况下它没有任何用处。我认为这与在闭包中向按钮添加一个自我作为目标有关。如果您是在实例化之后完成的,则在init调用中。我想可以。我对collectionView/tableView也这样做。如果成功了,你能把答案标记为正确吗:PI保证一旦我获得了这样做的声誉,我会投票给你的答案:D解释发生了什么:你用来定义
let
值的函数根本没有访问类的权限(在本例中是ViewController)。该函数中的
self
不是VC,请尝试在其中添加
print(“self:\(String(description:self)))”
。原因是:现在还没有VC。当你把它变成一个懒惰的变量时,有一个VC,self引用它。