gpt4 book ai didi

objective-c - 用户使用 UISegmentedControl 更改显示单位后立即更改显示单位

转载 作者:行者123 更新时间:2023-11-28 20:30:11 25 4
gpt4 key购买 nike

我有一个处理温度单位 (ºC/ºF) 设置的 UISegmentedControl。
它工作正常,但如果用户更改设置,显示的单元将不会更改(自行刷新),除非用户导航到另一个 View Controller ,然后返回到有问题的 View Controller 。

我在用

- (void)viewWillAppear:(BOOL)animated
{
//Gets user setting choice for Fahrenheit or Celsius
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
temperatureSetting = [[preferences valueForKey:@"temperature setting"]intValue];
//For getting the current value of temperature from the JSON data
NSInteger temp = [[conditions objectForKey:@"temperature"]intValue];
//For checking the user's setting to display either Fahrenheit or Celsius
if (temperatureSetting == 0) {
temp = (temp * 1.8) + 32;
temperatureLabel.text = [NSString stringWithFormat:@"%i°F", temp];
} else {
temperatureLabel.text = [NSString stringWithFormat: @"%iºC", temp];
}
}


- (IBAction)temperatureSelection:(UISegmentedControl *)sender {
//Used to save settings
NSUserDefaults *temperaturePreference = [NSUserDefaults standardUserDefaults];
//To check which segment is selected and save the value but also remember it
if ([sender selectedSegmentIndex] == 0) {
[temperaturePreference setInteger:0 forKey:@"temperature setting"];
[selectedSegment setInteger:0 forKey:@"segment setting"];
} else if ([sender selectedSegmentIndex] == 1) {
[temperaturePreference setInteger:1 forKey:@"temperature setting"];
[selectedSegment setInteger:1 forKey:@"segment setting"];
}
//To make sure the setting is saved
[temperaturePreference synchronize];
[selectedSegment synchronize];

}

如何让它在用户更改后立即更改显示的单位?
谢谢!

最佳答案

viewWillAppear: 中的代码移动到一个单独的方法中。当用户更改设置时调用该方法。

关于objective-c - 用户使用 UISegmentedControl 更改显示单位后立即更改显示单位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12446403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com