Search code examples
iosmmdrawercontroller

Mark mobile app menu button when there are new items


There are few ViewControllers in my app and all of them have menu button. When this button is pressed - menu ViewController is opened.

I want to mark menu button with red dot showing that some new content is available and user need to press menu button to see which one of menu item is marked with this dot.

As all my buttons are independent of each other - I thought that I need to solve it this way

  1. Add red dot image on each menu button
  2. Make this dot hidden by default
  3. When each ViewController is opened - i should check - are there any new items available and switch isHidden property of this red dot image to false.

But maybe there is some more elegant way?


Solution

  • use NotificationCenter to notify the ui when new content available

    in the menu viewcontroller class:

    //put this in viewDidLoad
    NotificationCenter.default.addObserver(self.selector : #selector(menuviewcontroller.refresh(_:)),name:NSNotification.Name(rawValue:"showRedBtn"),object : nill)
    
    
    //create function refresh
    func refresh(_ notification : Notification)
    {
       //make the red dot visible
    }
    

    create class that listen if any content added and call the delegate in case of add by this line of code

    NotificationCenter.default.post(name : Notification.Name("showRedBtn"),object : nil , userInfo : nil)
    

    hope it's will help you