Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to mantain localization on a UIButton after

I am using NSLocalizedString function to change the language of a UIButton

reset.titleLabel.text=NSLocalizedString(@"Reset", nil);

I put this line in the viewDiLoad , viewWillAppear , viewDidAppear , viewDidDisappear and viewWillDisappear

when the View load , the localized version of the text appear , when I click on the button the text change back to the original text that is on the button in the Storyboard and then return to the localized language again right before loading the following view (even the the titlelable never changes values all across these stages!)

I saw that one of the solutions is to have a localized version of the Storyboard but I don t want to do that yest since I will have to maintain all localized versions ... any other solution ?

like image 352
user1415780 Avatar asked Dec 20 '22 20:12

user1415780


1 Answers

I would suggest using -setTitle:forState: to change the title. By manually changing the titleLabel.text, you should expect the text to be reset occasionally. Most documentation examples just show manipulating the font size, etc. not the contents, and that is because the titleLabel is set to the currentTitle Since there are multiple states for buttons, iOS has a way to set these where they are stored and to make sure that any image caches are appropriately updated.

[reset setTitle: NSLocalizedString( @"Reset", nil) forState: UIControlStateNormal];

As long as none of the other states are explicitly set, this will be used for all of the states. If you want to change the title for different states, use one of the other state contents.

like image 120
gaige Avatar answered Jan 05 '23 18:01

gaige