将目标添加到Swift中的按钮

将目标添加到Swift中的按钮,swift,uibutton,Swift,Uibutton,我想在不同的情况下实现一个按钮的不同目标 如果是当前用户自己的配置文件,则按钮应添加目标“转到设置”。如果它是另一个用户,我想添加一个follow/unfollow操作 检查完所有参数后,我的代码应该可以运行了。但它不起作用。 我添加了一些照片,但按钮不可点击 func setupUserInformation(user: UserModel) { usernameLabel.text = user.username if user.uid == UserApi.shared.

我想在不同的情况下实现一个按钮的不同目标

如果是当前用户自己的配置文件,则按钮应添加目标“转到设置”。如果它是另一个用户,我想添加一个follow/unfollow操作

检查完所有参数后,我的代码应该可以运行了。但它不起作用。 我添加了一些照片,但按钮不可点击

func setupUserInformation(user: UserModel) {
    usernameLabel.text = user.username

    if user.uid == UserApi.shared.CURRENT_USER_ID {
        editButton.setTitle("Einstellungen", for: .normal)

        editButton.addTarget(self, action: #selector(goToSettings), for: .touchUpInside)

    } else {
        if user.isFollowing! == true {
            setupUnfollowButton()
        } else {
            setupFollowButton()
        }
    }
}

@objc func goToSettings() {
    delegate?.goToSettingVC()
}

func setupFollowButton() {
    editButton.layer.borderWidth = 1
    editButton.layer.borderColor = UIColor.lightGray.cgColor
    editButton.layer.cornerRadius = 5
    editButton.clipsToBounds = true

    editButton.setTitleColor(UIColor.white, for: .normal)
    editButton.backgroundColor = UIColor(red: 1 / 255, green: 84 / 255, blue: 147 / 255, alpha: 1.0)
    editButton.setTitle("Folgen", for: UIControl.State.normal)
    editButton.addTarget(self, action: #selector(followAction), for: .touchUpInside)
}

@objc func followAction() {
    print("Button wurde gedrückt")
    if user?.isFollowing == false {
        FollowApi.shared.followAction(withUser: user!.uid!)
        setupUnfollowButton()
        user?.isFollowing = true

    }
}

func setupUnfollowButton() {
    editButton.layer.borderWidth = 1
    editButton.layer.borderColor = UIColor.lightGray.cgColor
    editButton.layer.cornerRadius = 5
    editButton.clipsToBounds = true

    editButton.setTitleColor(UIColor.black, for: .normal)
    editButton.backgroundColor = UIColor.white
    editButton.setTitle("Entfolgen", for: UIControl.State.normal)
    editButton.addTarget(self, action: #selector(unFollowAction), for: .touchUpInside)
}

@objc func unFollowAction() {
    print("Button wurde gedrückt")
    if user?.isFollowing == true {
        FollowApi.shared.unfollowAction(withUser: user!.uid!)
        setupFollowButton()
        user?.isFollowing = false

    }
}
非常感谢您的帮助

更新

在下面的答案中做了一些更改。但add目标仍将无法实现

var user: UserModel? {
    didSet {
        guard let _user = user else { return }
        setupUserInformation(user: _user)
    }
}

func setupUserInformation(user: UserModel) {

    editButton.addTarget(self, action: #selector(didTapEditButton), for: .touchUpInside)

    if user.uid == UserApi.shared.CURRENT_USER_ID {
        editButton.setTitle("Einstellungen", for: .normal)

    }else {
        if user.isFollowing! == true {
            setupUnfollowButton()
        } else {
            setupFollowButton()
        }
    }

}

@objc func didTapEditButton() {

    //Add IF conditions according to what your button should do in different cases here.
    if user!.uid == UserApi.shared.CURRENT_USER_ID {
        editButton.setTitle("Einstellungen", for: .normal)
        goToSettings()
    }else {
        if user!.isFollowing! == true {
            unFollowAction()
        } else {
            followAction()
        }
    }

}

@objc func goToSettings() {
    delegate?.goToSettingVC()
}

func setupFollowButton() {
    editButton.layer.borderWidth = 1
    editButton.layer.borderColor = UIColor.lightGray.cgColor
    editButton.layer.cornerRadius = 5
    editButton.clipsToBounds = true

    editButton.setTitleColor(UIColor.white, for: .normal)
    editButton.backgroundColor = UIColor(red: 1 / 255, green: 84 / 255, blue: 147 / 255, alpha: 1.0)
    editButton.setTitle("Folgen", for: UIControl.State.normal)
}

func followAction() {
    print("Button wurde gedrückt")
    if user?.isFollowing == false {
        FollowApi.shared.followAction(withUser: user!.uid!)
        setupUnfollowButton()
        user?.isFollowing = true

    }
}

func setupUnfollowButton() {
    editButton.layer.borderWidth = 1
    editButton.layer.borderColor = UIColor.lightGray.cgColor
    editButton.layer.cornerRadius = 5
    editButton.clipsToBounds = true

    editButton.setTitleColor(UIColor.black, for: .normal)
    editButton.backgroundColor = UIColor.white
    editButton.setTitle("Entfolgen", for: UIControl.State.normal)
}

func unFollowAction() {
    print("Button wurde gedrückt")
    if user?.isFollowing == true {
        FollowApi.shared.unfollowAction(withUser: user!.uid!)
        setupFollowButton()
        user?.isFollowing = false

    }
}

请注意,添加目标不会执行操作代码;它只是设置了一个按钮,供用户稍后点击时使用


一旦你意识到这一点,你可能会发现你的整个方法可能是个坏主意。不要试图有条件地设定目标和行动。相反,只需一劳永逸地设定目标和行动。然后在action方法中,您可以设置条件来决定在点击按钮时执行的操作

马特是对的!!你的方法在这里是完全错误的。请尝试修改您的方法

您的方法应该是这样的:

override func viewDidLoad() {
    super.viewDidLoad()

    //Set Target to your button only once.
    editButton.addTarget(self, action: #selector(didTapEditButton), for: .touchUpInside)

}

@objc func didTapEditButton() {

    //Add IF conditions according to what your button should do in different cases here. 
    if user.uid == UserApi.shared.CURRENT_USER_ID {
        goToSettings()
    }else {
        if user.isFollowing! == true {
            setupUnfollowButton()
        } else {
            setupFollowButton()
        }
    }

}


func setupFollowButton() {

    //Make the check to see what state your button is currently in by checking if the title of the button is "Follow" or "Unfollow"
    if editButton.titleLabel?.text == "Unfollow"{
        editButton.layer.borderWidth = 1
        editButton.layer.borderColor = UIColor.lightGray.cgColor
        editButton.layer.cornerRadius = 5
        editButton.clipsToBounds = true

        editButton.setTitleColor(UIColor.white, for: .normal)
        editButton.backgroundColor = UIColor(red: 1 / 255, green: 84 / 255, blue: 147 / 255, alpha: 1.0)
        editButton.setTitle("Follow", for: UIControl.State.normal)

        followAction()
    }
}


 func setupUnfollowButton() {

    //Do the same here for the other state.
    if editButton.titleLabel?.text == "Follow"{
        editButton.layer.borderWidth = 1
        editButton.layer.borderColor = UIColor.lightGray.cgColor
        editButton.layer.cornerRadius = 5
        editButton.clipsToBounds = true

        editButton.setTitleColor(UIColor.black, for: .normal)
        editButton.backgroundColor = UIColor.white
        editButton.setTitle("Unfollow", for: UIControl.State.normal)

        unFollowAction()
    }
}


func followAction() {
    print("Button wurde gedrückt")
    if user?.isFollowing == false {
        FollowApi.shared.followAction(withUser: user!.uid!)
        setupUnfollowButton()
        user?.isFollowing = true

    }
}

func unFollowAction() {
    print("Button wurde gedrückt")
    if user?.isFollowing == true {
        FollowApi.shared.unfollowAction(withUser: user!.uid!)
        setupFollowButton()
        user?.isFollowing = false
    }
}

使用以下代码行解决了问题:

cell.contentView.isUserInteractionEnabled = false
只是忘了停用用户与集合视图的交互。当我点击按钮时,集合视图标题被激活,而不是按钮


谢谢你们的帮助

“它不工作”是什么意思?我的代码不执行addTarget。这是什么意思?您是否设置了断点并单步执行代码?是的。“editButton.setTitle(“Entfolgen”,代表:UIControl.State.normal)”正在我的应用程序中显示。但是代码不是通过“editButton.addTarget(self,action:#selector(unFollowAction),for:.touchUpInside)”运行的,并且打印不显示这是不可能的。如果Entfolgen行被执行,下一行将在它之后执行。感谢您的快速回答。如何将条件应用于实际方法?使用单词
if