使用MIBadgeButton_Swift将徽章添加到我的购物车图标这里是我的代码
badgeButton.badgeString = "\(cart.count)"
badgeButton.badgeTextColor = .whiteColor()
badgeButton.badgeEdgeInsets = UIEdgeInsetsMake(7, 0, -7, 34)
但是当我将新物品添加到购物车时,徽章不会增加。它不会更新徽章 Demo 弹出后,购物车文字不会更新
更新
func showAlertForRow(tt: String) {
let alert = SCLAlertView()
let txt = alert.addTextField("Quantity")
txt.keyboardType = UIKeyboardType.NumberPad
txt.text = "\(1)"
alert.addButton("Add to cart") {
let ff: Best!
let indexPath = self.tableView?.indexPathForSelectedRow
ff = self.best[indexPath!.row]
let app = UIApplication.sharedApplication().delegate as! AppDelegate
let context = app.managedObjectContext
let entity = NSEntityDescription.entityForName("Cart", inManagedObjectContext: context)
let cartt = Cart(entity: entity!, insertIntoManagedObjectContext: context)
print()
cartt.name = ff.name!
cartt.price = Double(ff.price!)
cartt.rest_id = "\(ff.rest_id)"
cartt.longg = ff.longg
cartt.latt = ff.latt
cartt.item_id = "\(ff.item_id)"
cartt.quantity = Int(txt.text!)
cartt.restName = ff.restName
cartt.cookTime = "\(ff.cookTime)"
var lt = String(ff.latt)
var lg = String(ff.longg)
var urlo = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=\(lt),\(lg)&destinations=\(LATITUDE!),\(LONGITUDE!)&mode=driving&language=pl-PLe"
print(urlo)
var url = NSURL(string: urlo)
let session = NSURLSession.sharedSession()
session.dataTaskWithURL(url!) {
(data, response, error) in
let jsonObj = JSON(data: data!)
let yy = jsonObj["rows"][0]["elements"][0]["distance"]["value"].int
var converted:Int?
if yy <= 5000{
converted = (yy! / 1000) * 80
}else{
converted = (yy! / 1000) * 65
}
print("CONVERTED IS \(converted!)")
if converted < 400{
converted = 400
}
cartt.delivery = converted
print("TOTALAMT\(converted)")
}.resume()
if let imageUrl = ff.imageString{
cartt.imageUrl = imageUrl
}
//img is from the outlet
//save
context.insertObject(cartt)
do{
try context.save()
print(cartt.longg)
print("saved")
}catch{
print("could not save ahaha")
}
self.navigationController?.popViewControllerAnimated(true)
}
dispatch_async(dispatch_get_main_queue()) {
self.badgeButton.badgeString = "\(self.cart.count)"
print("IT GOT HERE")
self.updateBadge()
}
let imagBu = UIImage(named: "shopping_cart_loaded.png")
alert.showSuccess("", subTitle: "Add \(tt) to cart", circleIconImage: imagBu, closeButtonTitle: "Close")
}