【问题标题】:Swift sender UIButton change textSwift 发送者 UIButton 更改文本
【发布时间】:2018-09-28 02:16:10
【问题描述】:

我有一个关注/取消关注按钮,并通过“发件人”访问它。当用户点击它以关注或取消关注另一个用户时,我正在更改文本。问题是当它应该显示“取消关注”时,它显示的是故事板中使用的默认文本。该按钮应更改为“关注”,而不是“取消关注”。另外,我必须使用“sender:UIButton”,因为我正在访问 tableview 单元格“标签”以获取正确的信息。

@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()
        }
    })



}

【问题讨论】:

  • 首先,看起来你正在倒退......在你的代码中,你设置了isFollower = true,然后将按钮标题设置为“follow”,然后if !isFollower(意思是,如果isFollower 为假)将标题设置为“取消关注”?似乎应该反过来。此外,您应该使用 sender.setTitle("follow", for: .normal) 而不是设置按钮标签的文本。
  • 我建议去掉所有无关的代码(你包括了几十行)然后测试。它可能会暴露实际问题?当然,如果没有,请发布 代码,以便我们可以复制内容。

标签: ios swift uibutton sender


【解决方案1】:

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

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

你在按钮动作中声明变量isFollowinside。这意味着每次无论关注还是取消关注,您的isFollower 都是false,这就是为什么关注条件会起作用的原因。但是在后续完成中对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
}

【讨论】:

  • 如果你使用了 isFollow 不需要指定类型,Swift 会推断
  • @AbidNawaz 是的。为了整洁和清晰,我已经养成了在变量旁边使用类型的习惯。
  • 我不认为这是正确的...... OP 确实在 func 中声明 var isFollower = false,但如果满足条件,则代码包含 @987654333 行@。因此,当代码执行到 if !isFollower { 行时,可能该值已更改。
  • @DonMag 是的,我仔细阅读了它,并且仍在尝试理解他的代码的作用。他似乎有一个 API 调用,如果成功,他将按照以下方式处理。那么,这是否意味着取消关注不需要任何 API 调用?或者他应该在 API 调用中反转isFollower。如果是这样,为什么他的取消关注逻辑在外面?
  • @LukasBimba 调用 API 的目的是什么?更新关注取消关注状态?
猜你喜欢
  • 1970-01-01
  • 2012-07-10
  • 1970-01-01
  • 1970-01-01
  • 2015-02-02
  • 1970-01-01
  • 2015-09-29
  • 2016-09-07
相关资源
最近更新 更多