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

标签 ios swift uibutton

出于某种原因,我制作了一个带有 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/

相关文章:

ios - 如何在一段时间内更改UIButton的颜色

ios - UILocalNotification 始终提供默认声音

ios - swift 3 : Is there a way to cast an object to a class and protocol at the same time?

swift - 如何在 macOS 的 SwiftUI 中使用菜单命令实现多窗口?

IOS Firebase 存储文件上传 - 对象不存在

json - 如何给一些时间(60 秒)快速重新发送 OTP 按钮

ios - 在 TableViewCell 中启用 UIButton 可在更多 Cell 中启用

ios - 为什么 UIAlertController 会用 self 创建一个保留周期?

javascript - React-Native 中的 NavigatorIOS 错误?

ios - addTarget 不触发任何事件