单击按钮后swift无法调用函数

时间:2018-05-13 22:18:31

标签: ios swift uibutton

我在模拟器中运行应用程序时出于某种原因制作了带有图像视图和内部按钮的容器视图我无法单击按钮并调用该功能将我带到下一个屏幕,我遇到了这种问题之前和它一样简单,只需将按钮更改为懒惰变量虽然在这种情况下不起作用?

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)

}

1 个答案:

答案 0 :(得分:0)

好吧,代码看起来很好,你只需要添加@objc

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

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

    }

<强>原因

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