iOS:如何“刷新”弹出其子 UIViewController 后的 UIViewController?

发布于 2024-11-27 09:33:21 字数 534 浏览 1 评论 0原文

在我的应用程序中,我使用 UINavigationController。我有一个“父” UIViewController 和一个“子” UIViewController 在堆栈上运行。用户可以对子级进行一些设置,这些设置稍后会影响父级。我使用 NSUserDefaults 来保存和检索数据,根据 NSLog,它似乎工作正常。

我不清楚的是,一旦我从孩子那里回来,我应该如何“刷新”数据。

让我更具体地说:在子项中有一个“后退”按钮,可以执行 popViewControllerAnimated,然后我们返回到父项。我想重新运行 viewDidLoad 中的所有方法,以便使用从 NSUserDefaults 数据获得的更改来设置父视图字段。

  1. 我应该在父方法中的哪里告诉视图“刷新”?
  2. 我该如何执行此刷新操作?我应该再次调用viewDidLoad吗?我读到了有关 setNeedsDisplay 的内容,如果这是我应该使用的东西,语法是什么(是“[self.view setNeedsDisplay]”还是其他内容)?

有人可以指导和详细说明吗?

In my app I'm using UINavigationController. I have a "parent" UIViewController and a "child" UIViewController running on the stack. The user can do some settings on the child that are later on suppose to affect the parent. I use NSUserDefaults to save and retrieve the data, and it seems to be working fine according to the NSLog.

What I'm not clear about is how am I supposed to "refresh" the data once I come back from the child.

Let me be more specific: In the child there is a "Back" button that does popViewControllerAnimated and then we go back to the parent. I want to re-run all the method I have in viewDidLoad so the parent view fields are set with the changes I got from the NSUserDefaults data.

  1. Where in the parent methods am I supposed to tell the view to "refresh"?
  2. How do I do this refresh action? should I call viewDidLoad again? I read about something called setNeedsDisplay, if that is the thing I should use, what is the syntax (is it "[self. view setNeedsDisplay]" or something else)?

Can anyone direct and elaborate?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疯到世界奔溃 2024-12-04 09:33:21

看看 NSNotification - 这是一种将更新从代码的一部分发送到另一部分的简单方法,这是 Apple 的内置 NSNotification 系统。

  1. 如果您有要发送的更新,请调用 postNotificationName。您只需为其提供一个您编写的唯一字符串(例如“com.razeware.imagegrabber.imageupdated”)和一个对象(例如刚刚下载完图像的 ImageInfo)。

  2. 如果您想了解此更新何时发生,请调用addObserver:selector:name:object。在我们的例子中,ImageListViewController 将想知道何时发生这种情况,以便它可以重新加载适当的表视图单元格。最好将其放在 viewDidLoad 中。

  3. 当视图被卸载时,不要忘记调用removeObserver:name:object。否则,通知系统可能会尝试在已卸载的视图(或更糟糕的是未分配的对象)上调用方法,这将是一件坏事!
    通过 Ray Wenderlich 博客

Take a look at NSNotification - thats an easy way to send updates from one part of your code to another is Apple’s built-in NSNotification system.

  1. If you have an update you want to send, you call postNotificationName. You just give it a unique string you make up (such as “com.razeware.imagegrabber.imageupdated”) and an object (such as the ImageInfo that just finished downloading its image).

  2. If you want to find out when this update happens, you call addObserver:selector:name:object. In our case the ImageListViewController will want to know when this happens so it can reload the appropriate table view cell. A good spot to put this is in viewDidLoad.

  3. Don’t forget to call removeObserver:name:object when the view gets unloaded. Otherwise, the notification system might try to call a method on an unloaded view (or worse an unallocated object), which would be a bad thing!
    via Ray Wenderlich blog

别理我 2024-12-04 09:33:21

您可以使用 NSUserDefaultsDidChangeNotification 作为刷新根视图控制器的触发器。

You could use NSUserDefaultsDidChangeNotification as a trigger for refreshing the root view controller.

眼藏柔 2024-12-04 09:33:21

您还可以将 ViewController 设置为 NavigationViewController 的委托

self.navigationController.delegate = self;

然后您将在方法中收到委托调用:

- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController 
                    animated:(BOOL)animated {
    NSLog(@"Did Show controller %@", viewController);
    // Return from child
    if ([viewController isKindOfClass:[SRPeopleViewController class]]) {
        [self loadData];
    }

}

此解决方案的小缺点(或成本)是您将收到比您想要的更多的调用,因此有必要进行过滤。

You could also set your ViewController as a delegate to NavigationViewController

self.navigationController.delegate = self;

Then you will receive delegate calls in method:

- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController 
                    animated:(BOOL)animated {
    NSLog(@"Did Show controller %@", viewController);
    // Return from child
    if ([viewController isKindOfClass:[SRPeopleViewController class]]) {
        [self loadData];
    }

}

Small drawback - or cost - of this solution is that you will receive more calls than you want, so filtering is necessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文