0

在我的应用程序的开始屏幕中,您可以通过单击英国或瑞典的国旗在英语和瑞典语之间选择语言。问题是当您单击按钮时 ViewDidLoad 无法识别 NSUserDefaults 中的更改。但是当您重新启动应用程序时,该语言是您单击的最新标志!所以它保存了 NSUserDefault 但它只在 ViewDidLoad 中第一次加载它。

当你点击英国国旗时,sprakval 设置为 0,如果你点击瑞典国旗,sprakval 设置为 1。当你点击一个旗帜时,它会变成一个在旗帜前面带有勾号图标的图像。

代码:

-(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.
     }
4

2 回答 2

1

viewDidLoad 是从 nib 文件加载视图后调用的方法。您不应该手动调用它。

如果您已编写代码以刷新 viewDidLoad 中的控件,请将其移至不同的方法并从您的按钮事件处理程序中调用该方法。

- (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];
}

按钮事件处理程序

-(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];
 }

编辑:由于您使用的是基于 tabBar 的应用程序,因此最好使用 viewWillAppear 重新加载特定于语言的控件

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

- (void)viewDidLoad
{
    [super viewDidLoad];
}
于 2013-04-21T18:08:14.877 回答
1

这是因为您正在通过其超类 in-(IBAction) sprakSE 和方法调用 ViewDidload-(IBAction) sprakEN 方法。因此,在这两种方法中都替换 [super viewDidLoad]; [self viewDidLoad];。它会正常工作。

希望它可以帮助你。

于 2013-04-21T18:13:13.647 回答