@IBAction func addToCart(sender: AnyObject) {
let itemObjectTitle = itemObject.valueForKey("itemDescription") as! String
let alertController = UIAlertController(title: "Add \(itemObjectTitle) to cart?", message: "", preferredStyle: .Alert)
let yesAction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default) { (action) in
var tabArray = self.tabBarController?.tabBar.items as NSArray!
var tabItem = tabArray.objectAtIndex(1) as! UITabBarItem
let badgeValue = "1"
if let x = badgeValue.toInt() {
tabItem.badgeValue = "\(x)"
}
}
我不知道为什么我不能直接做+=“(x)”
错误:二元运算符“+=”不能应用于“String?”与“String”类型得操作数
我希望每次用户选择“是”时它都增加1。现在显然它只是停留在1。
2条答案
按热度按时间xdnvmnnf1#
您可以尝试访问badgeValue并将其转换为Integer,如下所示:
雨燕2
Swift 3或更高版本
要删除徽章,只需将nil赋给覆盖viewDidAppear方法的badgeValue:
zysjyyx42#
与Swift 2配合使用: