1

UIApplication.sharedApplication()のプロパティを何らかの形で観察することは可能でしょうかswiftスイフトはUIApplicationプロパティを確認します

UIApplication.sharedApplication().applicationIconBadgeNumber私のアプリを更新するには、その番号に基づいてUIを更新する必要がありますが、このプロパティが変更されたときはいつでも、UIは影響を受けません。

このについての私のコードは:

private var badgeCount: String = String(UIApplication.sharedApplication().applicationIconBadgeNumber) 

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 

    self.notificationsButton.setTitle(self.badgeCount, forState: .Normal) 
} 

答えて

2

これは解決策であるあなたがiconBadgeNumberを追加するために働くことは、拡張子を経由してのUIApplicationに変数を計算し、それが本当の変数を設定し、変更

extension UIApplication 
{ 
    var iconBadgeNumber:Int { set{self.applicationIconBadgeNumber = iconBadgeNumber 
      NSNotificationCenter.defaultCenter().postNotificationName("BageNumberChanged", object: nil) 
     } get{return self.applicationIconBadgeNumber}} 
} 

//Add this at Observer 
NSNotificationCenter.defaultCenter().addObserver(self, selector: #Selector(Controller.Method), name: "BageNumberChanged", object: nil) 
0
のあなたのアプリを通知します

enter image description here

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    // Override point for customization after application launch. 


    UIApplication.sharedApplication().applicationIconBadgeNumber = 5 

    return true 
} 
override func viewWillAppear(animated: Bool) { 
     super.viewWillAppear(true) 

     let badgeCount: String = String(UIApplication.sharedApplication().applicationIconBadgeNumber) 

     let notificationsButton: UIButton = UIButton (type: UIButtonType.Custom) 

     notificationsButton.addTarget(self, action: "tapBarNotificationsButton", forControlEvents: UIControlEvents.TouchUpInside) 

     notificationsButton.frame = CGRectMake(0, 0, 25, 25) 

     notificationsButton.backgroundColor = UIColor.blackColor() 

     notificationsButton.setTitle(badgeCount, forState: .Normal) 

     let barnotificationsButton = UIBarButtonItem(customView: notificationsButton) 

     self.navigationItem.setRightBarButtonItem(barnotificationsButton, animated: true) 



    } 
関連する問題