【问题标题】:viewDidLoad not running when expectedviewDidLoad 未按预期运行
【发布时间】:2011-10-25 14:49:49
【问题描述】:

我的viewDidLoad 中有以下代码来更改图像:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if ([defaults objectForKey:@"colour"] == nil) {
        [defaults setObject:@"Red" forKey:@"colour"];
        [defaults setInteger:0 forKey:@"colourIndex"];
        [defaults synchronize];
    }


    [background setImage:[UIImage imageNamed:[defaults objectForKey:@"colour"]]];
    NSLog(@"%@", [defaults objectForKey:@"colour"]);
    // Logs "Blue"
}

当我在 iPhone 模拟器中打开翻转视图然后返回到我的原始视图时,NSLog 的结果不会改变,图像也不会改变。为什么会这样?

【问题讨论】:

    标签: objective-c ios cocoa-touch nslog viewdidload


    【解决方案1】:

    将此代码移至 viewWillAppear。每次呈现视图时都不会调用 viewDidLoad。

    这个链接提供了很好的解释。

    UIViewController viewDidLoad vs. viewWillAppear: What is the proper division of labor?

    【讨论】:

      猜你喜欢
      • 2014-11-12
      • 1970-01-01
      • 2020-06-28
      • 2012-02-18
      • 2018-01-18
      • 2012-06-14
      • 2019-03-03
      • 2012-09-21
      • 2014-07-19
      相关资源
      最近更新 更多