iOS中讓Settings Bundle中的變化立即在App中反應出來的兩種方法
為了能夠在Settings Bundle中的變化在進入App後能夠立即反應出來,我們必須牢牢守住一個位置:即當App從後台進入前台時.
我們有2種辦法在該時刻做一些讀取的操作,一種是在- (void)applicationWillEnterForeground:(UIApplication *)application方法中處理,一種是註冊UIApplicationWillEnterForegroundNotification通知.
我們分別來看一下,首先是applicationWillEnterForeground方法,很簡單,在其中做我們想要的:
- (void)applicationWillEnterForeground:(UIApplication *)application { [_viewController refreshFields];}
然後是UIApplicationWillEnterForegroundNotification通知,我們可以在視圖的某個進入方法裡調用:
-(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; [self refreshFields]; UIApplication *app = [UIApplication sharedApplication]; [[NSNotificationCenter defaultCenter]addObserver:self selector: @selector(applicationWillEnterFg:) name: UIApplicationWillEnterForegroundNotification object:app];}
最後是回調方法的實現:
-(void)applicationWillEnterFg:(NSNotification*)notification{ NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults synchronize]; [self refreshFields];}