swift - 将目标添加到 Swift 中的按钮

标签 swift uibutton

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

如果它是当前用户自己的个人资料,则该按钮应添加目标“转到设置”。如果是其他用户,我想添加关注/取消关注操作。

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

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

    }
}

最佳答案

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

你的方法应该是这样的:

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 - 将目标添加到 Swift 中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53546737/

相关文章:

swift - 是否可以自定义 fabric Twitter 登录按钮的大小?

ios - 如何正确设置 SCNView 高度的动画?

swift - UITextView 中的多个超链接 : Underlined and Bold, 粗体不起作用

ios - 无法使用户定义的运行时属性起作用

iphone - UIButton:需要一个圆形点击区域

iphone - 我们可以向 iphone native 地址簿 View Controller 添加额外的按钮吗?

iphone - 机芯不工作

ios - authorizationStatus 返回一个 bool 值

Swift 导航栏项目不调用操作

ios - 如何从存储在 Parse 列中的数组中删除对象