0

In the start screen of my app you are able to choose language between English and Swedish by clicking on either the flag of UK or Sweden. The problem is that the ViewDidLoad does not recognize changes in the NSUserDefaults when you click a button. But when you restart the app, the language is the latest flag you clicked! So it saves the NSUserDefault but it only loads it the first time in ViewDidLoad..

When you click the English flag, sprakval sets to 0, and if you click the swedish flag, sprakval sets to 1. When you click a flag, it changes to a image with a tick icon in front of the flag.

Code:

-(IBAction) sprakEN
 {
    sprakval=0;
    NSUserDefaults *sprakvalet = [NSUserDefaults standardUserDefaults];
    [sprakvalet setInteger:sprakval  forKey:@"Sprak "];
    [sprakvalet synchronize];

    [super viewDidLoad];
}



-(IBAction) sprakSE
 {
     sprakval=1;
     NSUserDefaults *sprakvalet = [NSUserDefaults standardUserDefaults];
     [sprakvalet setInteger:sprakval  forKey:@"Sprak "];
     [sprakvalet synchronize];

      [super viewDidLoad];
 }

  - (void)viewDidLoad
    {
        [super viewDidLoad];

        NSUserDefaults *sprakvalet = [NSUserDefaults standardUserDefaults];
        sprakval2  = [sprakvalet integerForKey:@"Sprak "];

        if (sprakval2==0)
        {
            spraklabel.text = [NSString stringWithFormat:@"Language:"];

            [lhb setTitle:@"english" forState:UIControlStateNormal];
            [hlb setTitle:@"english" forState:UIControlStateNormal];
            [fhb setTitle:@"english." forState:UIControlStateNormal];
            [blandatb setTitle:@"english.." forState:UIControlStateNormal];

            UIImage *encheck = [UIImage imageNamed:@"United_Kingdomchecked.png"];
            [enbutton setImage:encheck forState:UIControlStateNormal];

            UIImage *seuncheck = [UIImage imageNamed:@"Sweden.png"];
            [sebutton setImage:seuncheck forState:UIControlStateNormal];

            self.title = @"Game";
        }
        else if(sprakval2==1)
        {
            spraklabel.text = [NSString stringWithFormat:@"Språk:"];

            [lhb setTitle:@"swedish" forState:UIControlStateNormal];
            [hlb setTitle:@"swedish" forState:UIControlStateNormal];
            [flb setTitle:@"swedish" forState:UIControlStateNormal];
            [fhb setTitle:@"swedish" forState:UIControlStateNormal];
            [blandatb setTitle:@"swedish" forState:UIControlStateNormal];

            self.title = @"Spel";
            UIImage *secheck = [UIImage imageNamed:@"Swedenchecked.png"];
            [sebutton setImage:secheck forState:UIControlStateNormal];

            UIImage *enuncheck = [UIImage imageNamed:@"United_Kingdom.png"];
            [enbutton setImage:enuncheck forState:UIControlStateNormal];
        }

    // Do any additional setup after loading the view, typically from a nib.
     }
Anupdas
  • 10,211
  • 2
  • 35
  • 60
Mangy92
  • 621
  • 1
  • 10
  • 25

2 Answers2

1

viewDidLoad is a method invoked after view has been loaded from nib file. You are not supposed to call it manually.

If you have written the code to refresh the controls in viewDidLoad move that into a different method and invoke that method from your button event handler.

- (void)adjustControlsForLanguage
{
    NSUserDefaults *sprakvalet = [NSUserDefaults standardUserDefaults];
    sprakval2  = [sprakvalet integerForKey:@"Sprak "];

    if (sprakval2==0)
    {
        spraklabel.text = [NSString stringWithFormat:@"Language:"];

        [lhb setTitle:@"english" forState:UIControlStateNormal];
        [hlb setTitle:@"english" forState:UIControlStateNormal];
        [fhb setTitle:@"english." forState:UIControlStateNormal];
        [blandatb setTitle:@"english.." forState:UIControlStateNormal];

        UIImage *encheck = [UIImage imageNamed:@"United_Kingdomchecked.png"];
        [enbutton setImage:encheck forState:UIControlStateNormal];

        UIImage *seuncheck = [UIImage imageNamed:@"Sweden.png"];
        [sebutton setImage:seuncheck forState:UIControlStateNormal];

        self.title = @"Game";
    }
    else if(sprakval2==1)
    {
        spraklabel.text = [NSString stringWithFormat:@"Språk:"];

        [lhb setTitle:@"swedish" forState:UIControlStateNormal];
        [hlb setTitle:@"swedish" forState:UIControlStateNormal];
        [flb setTitle:@"swedish" forState:UIControlStateNormal];
        [fhb setTitle:@"swedish" forState:UIControlStateNormal];
        [blandatb setTitle:@"swedish" forState:UIControlStateNormal];

        self.title = @"Spel";
        UIImage *secheck = [UIImage imageNamed:@"Swedenchecked.png"];
        [sebutton setImage:secheck forState:UIControlStateNormal];

        UIImage *enuncheck = [UIImage imageNamed:@"United_Kingdom.png"];
        [enbutton setImage:enuncheck forState:UIControlStateNormal];
    }

}

viewDidLoad

- (void)viewDidLoad{
    [super viewDidLoad];
    [self adjustControlsForLanguage];
}

Button Event Handlers

-(IBAction) sprakEN {
    sprakval=0;
    NSUserDefaults *sprakvalet = [NSUserDefaults standardUserDefaults];
    [sprakvalet setInteger:sprakval  forKey:@"Sprak "];
    [sprakvalet synchronize];

    [self adjustControlsForLanguage];
}

-(IBAction) sprakSE {
     sprakval=1;
     NSUserDefaults *sprakvalet = [NSUserDefaults standardUserDefaults];
     [sprakvalet setInteger:sprakval  forKey:@"Sprak "];
     [sprakvalet synchronize];

      [self adjustControlsForLanguage];
 }

EDIT : Since you are using tabBar based app, it's better to use the viewWillAppear to reload the language specific controls

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self adjustControlsForLanguage];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}
Anupdas
  • 10,211
  • 2
  • 35
  • 60
  • Thanks! It works! But how do I get the language from another view? This startscreen is the first tab in a tab bar. When I try to get the language in the second tabs viewdidload, it only works the first time! When I go back and change the language and then back to tab two, the language doesn't change. But when I first enter a totally different view, and then go back and change the language, it also changes one time in the second tab! – Mangy92 Apr 21 '13 at 20:06
  • 1
    viewDidLoad method is only invoked once per viewController,ie first time they are loaded. When you click the second tab for the first time only this will be invoked. You can invoke the method adjustControlsForLanguage from viewWillAppear to remove sync issues. This method will be invoked everytime a view is shown. – Anupdas Apr 21 '13 at 20:13
  • I added the code you wrote, but I get a error saying something like "No visible @interface for"UIViewController" declares the selector "viewWillAppear"". Is there something I have to change? – Mangy92 Apr 21 '13 at 20:30
  • 1
    @user1344659 How silly of me, I have edited my answer viewWillAppear: has an animated param. I missed it. please try it now. – Anupdas Apr 21 '13 at 20:34
1

This is because you are calling ViewDidload method through its super class in -(IBAction) sprakSE and -(IBAction) sprakEN methods. So replace [super viewDidLoad]; with [self viewDidLoad]; in both methods. It will work properly.

Hope it helps you.

Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61
  • Thanks, It works! But how do I get the language from another view? This startscreen is the first tab in a tab bar. When I try to get the language in the second tabs viewdidload, it only works the first time! When I go back and change the language and then back to tab two, the language doesn't change. But when I first enter a totally different view, and then go back and change the language, it also changes one time in the second tab! – Mangy92 Apr 21 '13 at 20:06