当我在我使用Swift和xcode构建的应用程序中单击按钮时,它们将不起作用

问题描述 投票:0回答:2

我使用swift在我的应用程序的个人资料页面上添加了一些按钮,但是当我运行我的应用程序时,它们不起作用。它们出现在页面上,但不允许用户单击它们,我也不知道为什么这是我使用的代码:

在我的profileHeader类中:

let editProfileFollowButton: UIButton = {
    let button = UIButton(type: .system)
    button.setTitle("Loading", for: .normal)
    button.layer.cornerRadius = 5
    button.layer.borderColor = UIColor.black.cgColor
    button.backgroundColor = UIColor.black
    button.layer.borderWidth = 1
    button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
    button.setTitleColor(.black, for: .normal)
    button.addTarget(self, action: #selector(handleEditProfileFollowButton), for: .touchUpInside)
    return button
}()

@objc func handleEditProfileFollowButton() {
    delegate?.handleEditFollowTapped(for: self)
}

在我的个人资料中VC:

func handleEditFollowTapped(for header: ProfileHeader) {
       print("handle edit follow tapped")
   }

因此,当按下按钮时,它应该打印“轻触处理编辑”,但是它什么也不做,不知道为什么。

即使我在同一个班级也这样做:

let editProfileFollowButton: UIButton = {
    let button = UIButton(type: .system)
    button.setTitle("Loading", for: .normal)
    button.layer.cornerRadius = 5
    button.layer.borderColor = UIColor.black.cgColor
    button.backgroundColor = UIColor.black
    button.layer.borderWidth = 1
    button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
    button.setTitleColor(.black, for: .normal)
    button.addTarget(self, action: #selector(handleEditProfileFollowButton), for: .touchUpInside)
    return button
}()

@objc func handleEditProfileFollowButton() {
    print("handle edit follow tapped")
}

它不起作用。

我使用swift在我的应用程序的个人资料页面上添加了一些按钮,但是当我运行我的应用程序时,它们不起作用。它们出现在页面上,但不允许用户单击它们,我不知道为什么这是...

ios swift uibutton
2个回答
0
投票

您需要延迟初始化editProfileFollowButton,因为在声明editProfileFollowButton自身(您的情况下为ProfileHeader类)时仍然不可用。这是


0
投票

您如何显示此按钮?

© www.soinside.com 2019 - 2024. All rights reserved.