2016-05-14 66 views
0

如何在AppDelegate的UITabBar項目上設置徽章值。裏面的ViewController我寫這篇文章,以設置一個項目徽章值:在AppDelegate的UITabBar項目上設置徽章值

self.tabBarController?.tabBar.items?[3].badgeValue = String(noti_count) 

但我不知道如何在AppDelegate中訪問某個項目。我想設置索引3徽章值當我home鍵退出之後打開應用程序,應該在此方法中:

func applicationWillEnterForeground(application: UIApplication) 

回答

0

假設你的控制器的名稱是視圖控制器:

class ViewController: UIViewController { 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     // do whatever you want 
     NSNotificationCenter.defaultCenter().addObserver(
      self, 
      selector: #selector(ViewController.applicationWillEnterForeground(_:)), 
      name: UIApplicationWillEnterForegroundNotification, 
      object: nil) 
    } 
    func applicationWillEnterForeground(notification: NSNotification) { 
     print("∙ \(NSStringFromClass(self.dynamicType)) - applicationWillEnterForeground ") 
     // do whatever you want 
     self.tabBarController?.tabBar.items?[3].badgeValue = String(noti_count) 
    } 
}