2012-07-23 23 views
3

我目前在iPhone應用程序,使用NSLocale獲得當前貨幣符號工作正常,但當我按Home鍵該應用程序去後臺進程,我改變貨幣(設置>>國際>>地區格式>>像聯合國國家,印度等等)以這種方式,然後從後臺打開該應用程序的貨幣符號沒有改變,但我導航到另一個屏幕,然後來到前一個屏幕只有貨幣改變,但我想當我從後臺打開應用程序,那麼貨幣符號會自動更改。如何解決這個問題?我需要你的幫助如何在iPhone中刷新NSLocale?

感謝

我想在這裏代碼:

NSLocale *theLocale = [NSLocale currentLocale]; 
Getdollarsymbol = [theLocale objectForKey:NSLocaleCurrencySymbol]; 
NSString *Code = [theLocale objectForKey:NSLocaleCurrencyCode]; 
+0

這可能是你可以放手的東西。真實用戶不止一次更改語言環境的可能性很小。 這就是說,你在viewDidLoad或viewWillAppear中運行這段代碼嗎?我似乎回想起那裏可能存在背景差異。可能是錯誤的,但。 – 2012-07-23 14:18:01

+0

感謝您的回覆 – SampathKumar 2012-07-23 14:22:50

回答

9

請試試這段代碼。讓我知道這是工作,

-viewDidLoad:添加行:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshCurrencySymbol) name:NSCurrentLocaleDidChangeNotification object:nil]; 

則:

-(void) refreshCurrencySymbol 
{ 
    NSLocale *theLocale = [NSLocale currentLocale]; 
    NSString *symbol = [theLocale objectForKey:NSLocaleCurrencySymbol]; 
    NSLog(@"Symbol : %@",symbol); 
    NSString *code = [theLocale objectForKey:NSLocaleCurrencyCode]; 
    NSLog(@"Code : %@",code); 
} 

感謝。

+0

謝謝你Minu。我希望它對你有用。 – 2012-07-23 14:50:20

+0

確保您將每個addObserver與等效的removeObserver相匹配 – 2012-07-23 15:49:48

1

有關以下內容:

[NSLocale autoupdatingCurrentLocale] 

Apple docs提到

對象總是反映當前當前用戶的區域設置的狀態。

+0

感謝您的回覆 – SampathKumar 2012-07-23 14:21:24

+0

現在我試試這個。 – SampathKumar 2012-07-23 14:21:44

+0

同樣的問題正在發生。 – SampathKumar 2012-07-23 14:26:34