我想在不同情况下实现按钮的不同目标。
如果它是当前用户自己的个人资料,则该按钮应将目标“转到设置”。如果用户不同,我想添加一个关注/取消关注操作。
检查所有参数后,我的代码应该可以工作。但这是行不通的。 我添加了一些打印件,但该按钮不可单击。
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
}
}
在此先感谢您的帮助!
更新
进行一些更改,如以下答案所示。但是仍然不会执行添加目标。
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
}
}
答案 0 :(得分:3)
请注意,添加目标不会执行操作代码;它只是设置按钮以供用户稍后点击时使用。
一旦意识到这一点,您可能会发现这里的整个方法可能不是一个好主意。不要尝试有条件地设置目标和操作。而是一劳永逸地设定目标和行动。然后在action方法内部,您可以在其中放置条件来决定每当点击按钮时要做什么。
答案 1 :(得分:2)
马特是对的!!您的方法在这里是完全错误的。请尝试修改您的方法。
您的方法应该是这样的:
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
}
}
答案 2 :(得分:1)
使用以下代码行解决了该问题:
cell.contentView.isUserInteractionEnabled = false
只是忘记了停用用户与集合视图的交互。当我点击按钮时,集合视图标题已激活,而不是按钮。
谢谢大家的帮助!