简体   繁体   中英

How to set tabbar badge?

I'm simply trying to add a '!' when there is a new item and to have it removed once the tabbar is tapped. I'm at a loss of where to put the code in the following.

Essentially when there is a notification or a new conversation created, or an update to an existing conversation, I'd like a ! to pop up on the tabbar badge, and once the user taps said tabbar item the ! goes away.

func conversationViewController(viewController: ATLConversationViewController, didSendMessage message: LYRMessage) {
    println("Message sent!")

}




func conversationViewController(viewController: ATLConversationViewController, didFailSendingMessage message: LYRMessage, error: NSError?) {
    println("Message failed to sent with error: \(error)")
}

func conversationViewController(viewController: ATLConversationViewController, didSelectMessage message: LYRMessage) {
    println("Message selected")


}

// MARK - ATLConversationViewControllerDataSource methods

func conversationViewController(conversationViewController: ATLConversationViewController, participantForIdentifier participantIdentifier: String) -> ATLParticipant? {
    if (participantIdentifier == PFUser.currentUser()!.objectId!) {
        return PFUser.currentUser()!
    }
    let user: PFUser? = UserManager.sharedManager.cachedUserForUserID(participantIdentifier)
    if (user == nil) {
        UserManager.sharedManager.queryAndCacheUsersWithIDs([participantIdentifier]) { (participants: NSArray?, error: NSError?) -> Void in
            if (participants?.count > 0 && error == nil) {
                //self.addressBarController.reloadView()
                // TODO: Need a good way to refresh all the messages for the refreshed participants...

                self.reloadCellsForMessagesSentByParticipantWithIdentifier(participantIdentifier)

            } else {
                println("Error querying for users: \(error)")
            }
        }
    }
    return user
}

func conversationViewController(conversationViewController: ATLConversationViewController, attributedStringForDisplayOfDate date: NSDate) -> NSAttributedString? {
    let attributes: NSDictionary = [ NSFontAttributeName : UIFont.systemFontOfSize(14), NSForegroundColorAttributeName : UIColor.grayColor() ]
    return NSAttributedString(string: self.dateFormatter.stringFromDate(date), attributes: attributes as? [String : AnyObject])
}

func conversationViewController(conversationViewController: ATLConversationViewController, attributedStringForDisplayOfRecipientStatus recipientStatus: [NSObject:AnyObject]) -> NSAttributedString? {
    if (recipientStatus.count == 0) {
        return nil
    }
    let mergedStatuses: NSMutableAttributedString = NSMutableAttributedString()

    let recipientStatusDict = recipientStatus as NSDictionary
    let allKeys = recipientStatusDict.allKeys as NSArray
    allKeys.enumerateObjectsUsingBlock { participant, _, _ in
        let participantAsString = participant as! String
        if (participantAsString == self.layerClient.authenticatedUserID) {
            return
        }

        let checkmark: String = "✔︎"
        var textColor: UIColor = UIColor.lightGrayColor()
        let status: LYRRecipientStatus! = LYRRecipientStatus(rawValue: recipientStatusDict[participantAsString]!.unsignedIntegerValue)
        switch status! {
        case .Sent:
            textColor = UIColor.lightGrayColor()
        case .Delivered:
            textColor = UIColor.orangeColor()
        case .Read:
            textColor = UIColor.greenColor()
        default:
            textColor = UIColor.lightGrayColor()
        }
        let statusString: NSAttributedString = NSAttributedString(string: checkmark, attributes: [NSForegroundColorAttributeName: textColor])
        mergedStatuses.appendAttributedString(statusString)
    }
    return mergedStatuses;
}

// MARK - ATLAddressBarViewController Delegate methods methods


// MARK - ATLParticipantTableViewController Delegate Methods

func participantTableViewController(participantTableViewController: ATLParticipantTableViewController, didSelectParticipant participant: ATLParticipant) {
    println("participant: \(participant)")
    self.addressBarController.selectParticipant(participant)
    println("selectedParticipants: \(self.addressBarController.selectedParticipants)")
    self.navigationController!.dismissViewControllerAnimated(true, completion: nil)
}

func participantTableViewController(participantTableViewController: ATLParticipantTableViewController, didSearchWithString searchText: String, completion: ((Set<NSObject>!) -> Void)?) {
    UserManager.sharedManager.queryForUserWithName(searchText) { (participants, error) in
        if (error == nil) {
            if let callback = completion {
                callback(NSSet(array: participants as! [AnyObject]) as Set<NSObject>)
            }
        } else {
            println("Error search for participants: \(error)")
        }
    }
}

}

Solved https://stackoverflow.com/a/29837976/2303865

Let's say that you have queried the number of messages into a variable called counts then to show the this count into the first tabBarItem.

var arrayOfTabBar = self.tabBarController?.tabBar.items as NSArray!
let tabItem = arrayOfTabBar.objectAtIndex(1) as! UITabBarItem
tabItem.badgeValue =  counts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM