gpt4 book ai didi

iphone - viewWillAppear 困境

转载 作者:行者123 更新时间:2023-12-01 17:34:29 28 4
gpt4 key购买 nike

我正在使用 NSUserDefaults使一个对象在多个 UIViewController 之间保持同步UITabbarController 中使用的 s .为此,我正在实现以下

- (void)viewWillAppear:(BOOL)animated
{
NSLog(@"ViewControllerX Will Appear");
[super viewWillAppear:animated];

NSDictionary *dict = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"sharedDictionary"];
[customObject setDictionary:dict];
}


- (void)viewWillDisappear:(BOOL)animated
{
NSLog(@"ViewControllerX Will Disappear");
NSDictionary *dict = [customObject dictionary];
[[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"sharedDictionary"];
[[NSUserDefaults standardUserDefaults] synchronize];
}

这里 customObject是具有属性 dictionary 的自定义类的实例类型 NSDictionary .此对象可能会被可见的 UIViewController 更改.

我目前遇到的问题是,当用户切换标签时,比如说从 ViewControllerXViewControllerY ,这些方法没有按预期的顺序被调用。我希望在日志中看到这一点:
ViewControllerX Will Disappear
ViewControllerY Will Appear

但相反,我看到
ViewControllerY Will Appear
ViewControllerX Will Disappear

结果是旧字典加载到 ViewControllerY ,并且只有在再次切换标签后,新字典才会出现。有没有解决这个问题的简单方法?

最佳答案

无法保证将调用这些方法的顺序,因此您不能依赖它们的任何顺序。您得到的唯一保证是 -viewWillAppear:-viewWillDisappear:将在 View 分别出现或消失之前被调用。

处理这个问题的另一种方法可能是将其更改为 will/did 类型的场景。因此,您将对象的当前状态保存在 -viewWillDisappear: 中。然后在 -viewDidAppear: 中恢复状态(即加载字典) .这将保证即将消失的 View 在出现的 View 之前保存其字典。

另一种方法是更改​​自定义字典在 View Controller 之间传递的方式,并在应用程序的 UITabBarViewController 上使用委托(delegate)对象。处理将这些更改同步到用户默认值。您可以将其集成到您的应用程序中,但最有意义,但我将在下面提供一个基本示例以及您需要为您的应用程序提供的更改(如您的问题中所述):

要使用该示例,您需要进行以下更改(适应您的编码风格):

  • 添加 NSDictionary以 ivar 命名 _sharedDictionary给您的申请代表
  • 应用启动时从用户默认值加载字典
  • 声明您的应用委托(delegate)实现 UITabBarControllerDelegate协议(protocol)
  • 当您的应用程序加载时,将您的应用程序代理分配为您的主要代理UITabBarController .
  • 更改您的 View Controller 以响应您可以调用的新属性 sharedDictionary
  • 您可以在 -viewWillAppear 中维护其余代码您设置 View Controller 的 sharedDictionary 的值属性到您的自定义对象,然后像之前一样继续

  • 完成这些操作后,将以下方法实现添加到您的应用程序委托(delegate)中:
    -(BOOL)tabBarController:(UITabBarController*)tabBarController shouldSelectViewController:(UIViewController*)viewController
    {
    // If you're using ARC, you can remove the retain/autorelease statements
    [_sharedDictionary autorelease];

    // In order to avoid a compiler warning here, you should have your view controllers
    // possibly inherit from a parent that defines the sharedDictionary property and cast
    // to that, or have your view controllers implement a protocol that defines the
    // property, and cast to that. As long as your view controllers actually implement
    // the sharedDictionary property, however, everything will work
    _sharedDictionary = [[[tabBarController selectedViewController] sharedDictionary] retain];

    // set the shared dictionary on the new view controller -- same casting rules apply
    // as stated above
    [viewController setSharedDictionary:_sharedDictionary];

    // save this to user defaults so that if the app stops, it maintains whatever state
    // you're keeping
    dispatch_async(dispatch_get_main_queue(), ^{
    [[NSUserDefaults standardUserDefaults] setObject:_sharedDictionary forKey:@"sharedDictionary"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    });

    return YES;
    }

    关于iphone - viewWillAppear 困境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9057748/

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