使用UIAlertAction使用Swift增加Tab栏上的徽章

时间:2015-04-23 22:17:55

标签: ios swift

 @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 badgeNumber = 0

        self.navigationController!.tabBarItem.badgeValue == "\(badgeNumber++)"

    }
    let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil)
    alertController.addAction(yesAction)
    alertController.addAction(cancelAction)

    presentViewController(alertController, animated: true, completion: nil)

}

我需要选择"是"在每次按下时,在标签栏项目上增加徽章的警报,是否有人有这个问题?

1 个答案:

答案 0 :(得分:2)

let yesAction = UIAlertAction(title: "Yes", style: .Default) { action in
      // Your code to update the tab bar here
}

替代:

UIAlertAction(title: "Yes", 
              style: UIAlertActionStyle.Default, 
            handler: { // Code goes here 
})