gpt4 book ai didi

ios - 单击按钮后,Swift 无法调用函数

转载 作者:行者123 更新时间:2023-11-30 11:30:24 25 4
gpt4 key购买 nike

出于某种原因,我制作了一个带有 ImageView 和内部按钮的容器 View ,当在模拟器中运行应用程序时,我无法单击按钮并调用将我带到下一个屏幕的函数,我遇到了此类问题之前,就像将按钮更改为惰性变量一样简单,但这次不起作用?

lazy var nameLabelButton = UIButton()




func setupNavBarWithUser() {

guard let displayName = user?.DisplayName else { return }

let titleView = UIView()
titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
titleView.backgroundColor = UIColor.red

let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false
titleView.addSubview(containerView)

let profileImageView = UIImageView()
profileImageView.translatesAutoresizingMaskIntoConstraints = false
profileImageView.contentMode = .scaleAspectFill
profileImageView.layer.cornerRadius = 20
profileImageView.clipsToBounds = true
if let profileImageUrl = user?.profileImageURL {
profileImageView.loadImageUsingCacheWithUrlString(urlString:profileImageUrl)
}

containerView.addSubview(profileImageView)

//ios 9 constraint anchors
//need x,y,width,height anchors
profileImageView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
profileImageView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
profileImageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
profileImageView.heightAnchor.constraint(equalToConstant: 40).isActive = true


containerView.addSubview(nameLabelButton)
nameLabelButton.setTitle("\(displayName)", for: .normal)
nameLabelButton.setTitleColor(.black, for: .normal)
nameLabelButton.translatesAutoresizingMaskIntoConstraints = false
//need x,y,width,height anchors
nameLabelButton.leftAnchor.constraint(equalTo: profileImageView.rightAnchor, constant: 8).isActive = true
nameLabelButton.centerYAnchor.constraint(equalTo: profileImageView.centerYAnchor).isActive = true
nameLabelButton.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
nameLabelButton.heightAnchor.constraint(equalTo: profileImageView.heightAnchor).isActive = true

containerView.centerXAnchor.constraint(equalTo: titleView.centerXAnchor).isActive = true
containerView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = true

nameLabelButton.addTarget(self, action: #selector(self.openUsersProfileController), for: .touchUpInside)



self.navigationItem.titleView = titleView

}


func openUsersProfileController(){
print("asdasdadsad")

let openUsersProfileController = UserProfileController(collectionViewLayout: UICollectionViewFlowLayout())
openUsersProfileController.user = self.user
navigationController?.pushViewController(openUsersProfileController, animated: true)

}

最佳答案

嗯,代码看起来不错,你只需要添加 @objc

@objc func openUsersProfileController(){
print("asdasdadsad")

let openUsersProfileController = UserProfileController(collectionViewLayout: UICollectionViewFlowLayout())
openUsersProfileController.user = self.user
navigationController?.pushViewController(openUsersProfileController, animated: true)

}

原因:

Swift 4开始,我们需要在函数之前手动添加@objc

关于ios - 单击按钮后,Swift 无法调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50321099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com