1

swift içinde UIApplication.sharedApplication() özelliğinin nedenini gözlemlemek mümkün mü?Hızlı UIApplication özelliğini gözlemleyin

Uygulamayı numaralı telefon numarasına göre güncellemek için UIApplication.sharedApplication().applicationIconBadgeNumber kodunu izlemem gerekiyor, ancak bu özellik değiştirildiğinde kullanıcı arayüzü etkilenmiyor. Bu konuyla ilgili

Kodum:

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

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

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

cevap

2

Eklemek için bu çözüm iconBadgeNumber uzantısı üzerinden UIApplication değişkeni bilgisayarlı çalışabilir ve gerçek değişkeni ayarlamak ve değişikliklerden uygulamanızı bildirecektir

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) 



    }