在 Swift 中将目标添加到 Button [英] Add target to Button in Swift

查看:34
本文介绍了在 Swift 中将目标添加到 Button的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to implement different targets in different cases to a button.

如果是当前用户自己的配置文件,则按钮应添加目标转到设置".如果是不同的用户,我想添加一个关注/取消关注操作.

If its the own profile of the current user, the button should add the target "go to settings". If its a different user, I want to add a follow/unfollow action.

检查所有参数后,我的代码应该可以工作了.但它不起作用.我添加了一些打印,但按钮不可点击.

After checking all parameters, my code should work. But it doesn't work. I added some prints but the button is not clickable.

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

    }
}

非常感谢您的帮助!

更新

进行了一些更改,如下面的答案所示.但是仍然不会执行添加目标.

Made some changes as in the answers below. But still the add target will be not carried out.

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

    }
}

推荐答案

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

Matt is right!! your approach is totally wrong here. Please try to modify your approach.

你的方法应该是这样的:

Your approach should be something like this:

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
    }
}

这篇关于在 Swift 中将目标添加到 Button的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆