gpt4 book ai didi

ios - Swift 发送器 UIButton 更改文本

转载 作者:可可西里 更新时间:2023-11-01 01:08:21 26 4
gpt4 key购买 nike

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

@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 的更改不会在您下次单击按钮时反射(reflect)出来,因为您正在将 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
}

关于ios - Swift 发送器 UIButton 更改文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52542336/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com