Swift 发件人 UIButton 更改文本

Swift sender UIButton change text

我有一个 follow/unfollow 按钮,我正在通过 "sender" 访问它。当用户点击它以关注或取消关注另一个用户时,我正在更改文本。问题是当它应该显示 "unfollow" 时,它显示的是故事板中使用的默认文本。按钮变为 "follow",但不是 "unfollow"。此外,我必须使用 "sender: UIButton",因为我正在访问表格视图单元格 "tag" 以获取正确的信息。

@IBAction func followButton(_ sender: UIButton) {
    //self.yourFollowing.removeAll()
    //self.following.removeAll()
    self.followingTableView.reloadData()

    let accessData = self.yourFollowing[sender.tag].dataPass
    let businessUid = accessData["uid"] as! String
    let businessName = accessData["businessName"] as! String
    let businessStreet = accessData["businessStreet"] as! String
    let businessCity = accessData["businessCity"] as! String
    let businessState = accessData["businessState"] as! String
    let businessZip = accessData["businessZIP"] as! String
    let businessPhone = accessData["businessPhone"] as! String
    let businessLatitude = accessData["businessLatitude"] as! String
    let businessLongitude = accessData["businessLongitude"] as! String
    let businessWebsite = accessData["businessWebsite"] as! String

    let businessFacebook = accessData["facebookURL"] as! String
    let businessFoursquare = accessData["foursquareURL"] as! String
    let businessGoogle = accessData["googleURL"] as! String
    let businessInstagram = accessData["instagramURL"] as! String
    let businessSnapchat = accessData["snapchatURL"] as! String
    let businessTwitter = accessData["twitterURL"] as! String
    let businessYelp = accessData["yelpURL"] as! String


    let userID = Auth.auth().currentUser!.uid
    let ref = Database.database().reference()
    let key = ref.child("Businesses").childByAutoId().key
    var isFollower = false

    let followersRef = "followers/\(businessUid)/\(self.loggedInUserData?["uid"] as! String)"
    let followingRef = "following/" + (self.loggedInUserData?["uid"] as! String) + "/" + (businessUid)


    ref.child("Businesses").child(userID).child("following").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in

        if let following = snapshot.value as? [String : AnyObject] {
            for (item, value) in following {
                if value as! String == businessUid {
                    isFollower = true

                    let followersRef = "followers/\(businessUid)/\(self.loggedInUserData?["uid"] as! String)"
                    let followingRef = "following/" + (self.loggedInUserData?["uid"] as! String) + "/" + (businessUid)

                    let childUpdates = [followingRef:NSNull(),followersRef:NSNull()]
                    self.databaseRef.updateChildValues(childUpdates)

                    ref.child("Businesses").child(userID).child("following/\(item)").removeValue()
                    ref.child("Businesses").child(businessUid).child("followers/\(item)").removeValue()


                    sender.titleLabel?.text = "follow"



                    //self.yourFollowing.removeAll()
                    self.following.removeAll()
                    self.followingTableView.reloadData()
                }
            }
        }

        // Follow
        if !isFollower {

            sender.titleLabel?.text = "unfollow"

            let followersData = ["email":self.loggedInUserData?["email"] as! String, "businessName":self.loggedInUserData?["businessName"] as! String]
            let followingData = ["businessName":businessName, "businessStreet":businessStreet, "businessCity":businessCity, "businessState":businessState, "businessZIP":businessZip, "businessPhone":businessPhone, "businessWebsite":businessWebsite,"businessLatitude":businessLatitude, "businessLongitude":businessLongitude, "facebookURL":businessFacebook, "twitterURL":businessTwitter, "instagramURL":businessInstagram, "googleURL":businessGoogle, "yelpURL":businessYelp, "foursquareURL":businessFoursquare, "snapchatURL":businessSnapchat, "uid":businessUid]


            let childUpdates = [followersRef:followersData, followingRef:followingData]
            self.databaseRef.updateChildValues(childUpdates)

            let following = ["following/\(key)" : businessUid]
            let followers = ["followers/\(key)" : userID]

            ref.child("Businesses").child(userID).updateChildValues(following as Any as! [AnyHashable : Any])
            ref.child("Businesses").child(businessUid).updateChildValues(followers)




            self.yourFollowing.removeAll()
            self.following.removeAll()
            self.followingTableView.reloadData()
        }
    })



}

你的问题是按钮操作中的这一行

@IBAction func followButton(_ sender: UIButton) {
    .
    .
    var isFollower = false
    .
    .
}

您正在 isFollow 按钮操作中声明变量。这意味着每次无论关注还是取消关注,您的 isFollower 都是 false 这就是关注条件有效的原因。但是在 follow 完成后对 true 的更改将不会在您下次单击该按钮时反映出来,因为您正在将 isFollower 重置为 false

解决方法:将变量isFollow移到按钮动作之外。

var isFollow = false

@IBAction func followButton(_ sender: UIButton) {
     // Your logic
}

另外你的逻辑完成里面似乎错误。像下面的代码应该在那里将其更改为 false.

if value as! String == businessUid {
    isFollower = !isFollower
    if isFollower {
        // Follow logic
        sender.setTitle("unfollow", for: .normal)
    } else {
        // Unfollowed logic
        sender.setTitle("follow", for: .normal)
    }
    // Reload table
}