how to update UIBarButtonItem badge in swift 4?
I put badge in 'UINavigationBarButton'. when I come to home screen it show proper value. but when I go for that 'UINavigationBarButton' action I move to second viewcontroller. Here In viewcontroller I called API and that give me the count and in second viewcontroller I called API and that give also me value. In home viewcontroller I subtract both value and that value show into the badge. Now when I come back from second viewcontroller to home viewcontroller at that time badge count will be '0'. how this possible in ios swift ?
My HomeViewcontroller code is this
var bellButton : MIBadgeButton!
var bellButtonmage : UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
var webcount = 0
var notificationcount = 0
if UserDefaults.standard.object(forKey: "webcount") != nil{
webcount = UserDefaults.standard.object(forKey: "webcount") as! Int
}
if UserDefaults.standard.object(forKey: "oldcount") != nil{
notificationcount = UserDefaults.standard.object(forKey: "oldcount") as! Int
}
let remainCount = webcount - notificationcount
self.countnotificationAPI()
bellButton = MIBadgeButton.init(frame: CGRect.init(x: UIScreen.main.bounds.size.width-50, y: 0, width: 20, height: 20))
bellButton.badgeEdgeInsets = UIEdgeInsets.init(top: 5, left: 15, bottom: 0, right: 15)
bellButtonmage = UIImageView.init(frame: CGRect.init(x: 0, y: 5, width: 20, height: 20))
bellButtonmage.image = UIImage.init(named: "notification")
bellButton.addSubview(bellButtonmage)
bellButton.badgeString = String.init(format: "%d", remainCount)
bellButton.badgeTextColor = UIColor.white
bellButton.badgeBackgroundColor = UIColor.red
let rightbarbutton = UIBarButtonItem.init(customView: bellButton)
self.navigationItem.rightBarButtonItem = rightbarbutton
self.bellButton.addTarget(self, action: #selector(notificationaction), for:.touchUpInside)
}
@objc func notificationaction() {
self.performSegue(withIdentifier: "gotonotification", sender: nil)
}
func countnotificationAPI() {
let param = ["API" : "notification_count"]
Alamofire.request(url, method: .post, parameters: param, encoding: URLEncoding.default).responseJSON { (response) in
if let datastring = response.result.value as? [String:Any] {
let notificationcount = datastring["notification_count"] as! Int
UserDefaults.standard.set(notificationcount, forKey: "webcount")
UserDefaults.standard.synchronize()
}
}
}
}
In SecondViewcontroller code is
func notificationAPI() {
let param = ["API" : "notification_list"]
Alamofire.request(url, method: .post, parameters: param, encoding: URLEncoding.default).responseJSON { (response) in
if let datastring = response.result.value as? [String:Any] {
let notificationlist = datastring["notification_list"] as! [[String:Any]]
let oldcount = datastring["notification_count"]
UserDefaults.standard.set(oldcount, forKey: "oldcount")
UserDefaults.standard.synchronize()
}
}
}
My navigationBarButton is placed in homeviewcontroller.
ios swift
add a comment |
I put badge in 'UINavigationBarButton'. when I come to home screen it show proper value. but when I go for that 'UINavigationBarButton' action I move to second viewcontroller. Here In viewcontroller I called API and that give me the count and in second viewcontroller I called API and that give also me value. In home viewcontroller I subtract both value and that value show into the badge. Now when I come back from second viewcontroller to home viewcontroller at that time badge count will be '0'. how this possible in ios swift ?
My HomeViewcontroller code is this
var bellButton : MIBadgeButton!
var bellButtonmage : UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
var webcount = 0
var notificationcount = 0
if UserDefaults.standard.object(forKey: "webcount") != nil{
webcount = UserDefaults.standard.object(forKey: "webcount") as! Int
}
if UserDefaults.standard.object(forKey: "oldcount") != nil{
notificationcount = UserDefaults.standard.object(forKey: "oldcount") as! Int
}
let remainCount = webcount - notificationcount
self.countnotificationAPI()
bellButton = MIBadgeButton.init(frame: CGRect.init(x: UIScreen.main.bounds.size.width-50, y: 0, width: 20, height: 20))
bellButton.badgeEdgeInsets = UIEdgeInsets.init(top: 5, left: 15, bottom: 0, right: 15)
bellButtonmage = UIImageView.init(frame: CGRect.init(x: 0, y: 5, width: 20, height: 20))
bellButtonmage.image = UIImage.init(named: "notification")
bellButton.addSubview(bellButtonmage)
bellButton.badgeString = String.init(format: "%d", remainCount)
bellButton.badgeTextColor = UIColor.white
bellButton.badgeBackgroundColor = UIColor.red
let rightbarbutton = UIBarButtonItem.init(customView: bellButton)
self.navigationItem.rightBarButtonItem = rightbarbutton
self.bellButton.addTarget(self, action: #selector(notificationaction), for:.touchUpInside)
}
@objc func notificationaction() {
self.performSegue(withIdentifier: "gotonotification", sender: nil)
}
func countnotificationAPI() {
let param = ["API" : "notification_count"]
Alamofire.request(url, method: .post, parameters: param, encoding: URLEncoding.default).responseJSON { (response) in
if let datastring = response.result.value as? [String:Any] {
let notificationcount = datastring["notification_count"] as! Int
UserDefaults.standard.set(notificationcount, forKey: "webcount")
UserDefaults.standard.synchronize()
}
}
}
}
In SecondViewcontroller code is
func notificationAPI() {
let param = ["API" : "notification_list"]
Alamofire.request(url, method: .post, parameters: param, encoding: URLEncoding.default).responseJSON { (response) in
if let datastring = response.result.value as? [String:Any] {
let notificationlist = datastring["notification_list"] as! [[String:Any]]
let oldcount = datastring["notification_count"]
UserDefaults.standard.set(oldcount, forKey: "oldcount")
UserDefaults.standard.synchronize()
}
}
}
My navigationBarButton is placed in homeviewcontroller.
ios swift
add a comment |
I put badge in 'UINavigationBarButton'. when I come to home screen it show proper value. but when I go for that 'UINavigationBarButton' action I move to second viewcontroller. Here In viewcontroller I called API and that give me the count and in second viewcontroller I called API and that give also me value. In home viewcontroller I subtract both value and that value show into the badge. Now when I come back from second viewcontroller to home viewcontroller at that time badge count will be '0'. how this possible in ios swift ?
My HomeViewcontroller code is this
var bellButton : MIBadgeButton!
var bellButtonmage : UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
var webcount = 0
var notificationcount = 0
if UserDefaults.standard.object(forKey: "webcount") != nil{
webcount = UserDefaults.standard.object(forKey: "webcount") as! Int
}
if UserDefaults.standard.object(forKey: "oldcount") != nil{
notificationcount = UserDefaults.standard.object(forKey: "oldcount") as! Int
}
let remainCount = webcount - notificationcount
self.countnotificationAPI()
bellButton = MIBadgeButton.init(frame: CGRect.init(x: UIScreen.main.bounds.size.width-50, y: 0, width: 20, height: 20))
bellButton.badgeEdgeInsets = UIEdgeInsets.init(top: 5, left: 15, bottom: 0, right: 15)
bellButtonmage = UIImageView.init(frame: CGRect.init(x: 0, y: 5, width: 20, height: 20))
bellButtonmage.image = UIImage.init(named: "notification")
bellButton.addSubview(bellButtonmage)
bellButton.badgeString = String.init(format: "%d", remainCount)
bellButton.badgeTextColor = UIColor.white
bellButton.badgeBackgroundColor = UIColor.red
let rightbarbutton = UIBarButtonItem.init(customView: bellButton)
self.navigationItem.rightBarButtonItem = rightbarbutton
self.bellButton.addTarget(self, action: #selector(notificationaction), for:.touchUpInside)
}
@objc func notificationaction() {
self.performSegue(withIdentifier: "gotonotification", sender: nil)
}
func countnotificationAPI() {
let param = ["API" : "notification_count"]
Alamofire.request(url, method: .post, parameters: param, encoding: URLEncoding.default).responseJSON { (response) in
if let datastring = response.result.value as? [String:Any] {
let notificationcount = datastring["notification_count"] as! Int
UserDefaults.standard.set(notificationcount, forKey: "webcount")
UserDefaults.standard.synchronize()
}
}
}
}
In SecondViewcontroller code is
func notificationAPI() {
let param = ["API" : "notification_list"]
Alamofire.request(url, method: .post, parameters: param, encoding: URLEncoding.default).responseJSON { (response) in
if let datastring = response.result.value as? [String:Any] {
let notificationlist = datastring["notification_list"] as! [[String:Any]]
let oldcount = datastring["notification_count"]
UserDefaults.standard.set(oldcount, forKey: "oldcount")
UserDefaults.standard.synchronize()
}
}
}
My navigationBarButton is placed in homeviewcontroller.
ios swift
I put badge in 'UINavigationBarButton'. when I come to home screen it show proper value. but when I go for that 'UINavigationBarButton' action I move to second viewcontroller. Here In viewcontroller I called API and that give me the count and in second viewcontroller I called API and that give also me value. In home viewcontroller I subtract both value and that value show into the badge. Now when I come back from second viewcontroller to home viewcontroller at that time badge count will be '0'. how this possible in ios swift ?
My HomeViewcontroller code is this
var bellButton : MIBadgeButton!
var bellButtonmage : UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
var webcount = 0
var notificationcount = 0
if UserDefaults.standard.object(forKey: "webcount") != nil{
webcount = UserDefaults.standard.object(forKey: "webcount") as! Int
}
if UserDefaults.standard.object(forKey: "oldcount") != nil{
notificationcount = UserDefaults.standard.object(forKey: "oldcount") as! Int
}
let remainCount = webcount - notificationcount
self.countnotificationAPI()
bellButton = MIBadgeButton.init(frame: CGRect.init(x: UIScreen.main.bounds.size.width-50, y: 0, width: 20, height: 20))
bellButton.badgeEdgeInsets = UIEdgeInsets.init(top: 5, left: 15, bottom: 0, right: 15)
bellButtonmage = UIImageView.init(frame: CGRect.init(x: 0, y: 5, width: 20, height: 20))
bellButtonmage.image = UIImage.init(named: "notification")
bellButton.addSubview(bellButtonmage)
bellButton.badgeString = String.init(format: "%d", remainCount)
bellButton.badgeTextColor = UIColor.white
bellButton.badgeBackgroundColor = UIColor.red
let rightbarbutton = UIBarButtonItem.init(customView: bellButton)
self.navigationItem.rightBarButtonItem = rightbarbutton
self.bellButton.addTarget(self, action: #selector(notificationaction), for:.touchUpInside)
}
@objc func notificationaction() {
self.performSegue(withIdentifier: "gotonotification", sender: nil)
}
func countnotificationAPI() {
let param = ["API" : "notification_count"]
Alamofire.request(url, method: .post, parameters: param, encoding: URLEncoding.default).responseJSON { (response) in
if let datastring = response.result.value as? [String:Any] {
let notificationcount = datastring["notification_count"] as! Int
UserDefaults.standard.set(notificationcount, forKey: "webcount")
UserDefaults.standard.synchronize()
}
}
}
}
In SecondViewcontroller code is
func notificationAPI() {
let param = ["API" : "notification_list"]
Alamofire.request(url, method: .post, parameters: param, encoding: URLEncoding.default).responseJSON { (response) in
if let datastring = response.result.value as? [String:Any] {
let notificationlist = datastring["notification_list"] as! [[String:Any]]
let oldcount = datastring["notification_count"]
UserDefaults.standard.set(oldcount, forKey: "oldcount")
UserDefaults.standard.synchronize()
}
}
}
My navigationBarButton is placed in homeviewcontroller.
ios swift
ios swift
edited Nov 19 '18 at 4:42
sohan
asked Nov 17 '18 at 13:26
sohansohan
296
296
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Instead of viewDidLoad()
try your code in
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//update badge count here
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351681%2fhow-to-update-uibarbuttonitem-badge-in-swift-4%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Instead of viewDidLoad()
try your code in
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//update badge count here
}
add a comment |
Instead of viewDidLoad()
try your code in
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//update badge count here
}
add a comment |
Instead of viewDidLoad()
try your code in
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//update badge count here
}
Instead of viewDidLoad()
try your code in
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//update badge count here
}
answered Nov 19 '18 at 6:20
Shruti ThombreShruti Thombre
8331722
8331722
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351681%2fhow-to-update-uibarbuttonitem-badge-in-swift-4%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown