[selfmissModalViewControllerAnimated:YES]之后如何上传uiviewcontroller;

发布于 2024-12-14 10:48:54 字数 356 浏览 1 评论 0原文

我有一个 uiviewcontroller,它在 NSUserDefault 中保存一些 NSString。当我以这种方式返回

-(IBAction)goBack:(id)sender{
    [self dismissModalViewControllerAnimated:YES];
    [self viewWillAppear:YES];
}

到另一个必须使用 NSUserDefault 中保存的 NSString 的 uiviewcontroller 时。 在 viewWillApper 中,它加载这个 nsstring。当我第一次启动应用程序时,它会正确加载数据,如果我从另一个视图返回,则不会。我能做些什么?

i have an uiviewcontroller which save some NSString in NSUserDefault. When i go back in this way

-(IBAction)goBack:(id)sender{
    [self dismissModalViewControllerAnimated:YES];
    [self viewWillAppear:YES];
}

to another uiviewcontroller that have to use that NSString saved in NSUserDefault.
In viewWillApper it loads this nsstring. when i launch application the first time it load data correctly, if i turn back from another view no. What can i do?

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

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

发布评论

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

评论(1

脸赞 2024-12-21 10:48:54

将关心默认值的视图控制器注册为 NSUserDefaultsDidChangeNotification 的观察者。当您收到通知时,从默认值中获取值并使用它执行您需要的操作。

如果您只是呈现一个模态视图控制器,在其中设置字符串,并且父视图控制器需要知道新值,那么您可以在父视图控制器上拥有一个属性并将其设置为模态视图控制器被解雇。

我假设你的示例代码来自你的模态视图控制器 - 这对呈现视图控制器没有影响(你在模态控制器上调用 viewWillAppear ,因为你正在忽略它,这是一个坏主意)并且 viewWillAppear 不会被调用当视图控制器关闭模态视图控制器时。

要执行我的第二个建议,请在父视图控制器的标头中:

@property (nonatomic,copy) NSString *stringFromDefaults;

在 .m 文件中:

@synthesize stringFromDefaults;

在当前获取值的 viewWillAppear 中,将 in 分配给属性:

self.stringFromDefaults = [[NSUserDefaults standardUserDefaults] stringForKey:@"stringValue"];

从模态视图控制器中:

-(IBAction)goBack:(id)sender
{
    [(ParentViewController*)self.parentViewController setStringFromDefaults:newStringValue];
    [self dismissModalViewControllerAnimated:YES];
}

其中 newStringValue 是您也存储在默认值中的更新后的字符串值。

Register the view controller that cares about the value from defaults as an observer of the NSUserDefaultsDidChangeNotification. When you receive the notification, get the value from defaults and do whatever you need to with it.

If you are simply presenting a modal view controller in which you are setting the string, and it is the parent view controller that needs to know the new value, then you could just have a property on the parent view controller and set that as your modal view controller is dismissed.

The sample code you have there I assume is from your modal view controller - this will have no effect on the presenting view controller (you are calling viewWillAppear on the modal controller as you are dismissing it, a bad idea) and viewWillAppear is not called on a view controller when it has dismissed a modal view controller.

To do my second suggestion, in your parent view controller's header:

@property (nonatomic,copy) NSString *stringFromDefaults;

In the .m file:

@synthesize stringFromDefaults;

In your viewWillAppear, where you currently get the value, assign in to the property instead:

self.stringFromDefaults = [[NSUserDefaults standardUserDefaults] stringForKey:@"stringValue"];

From your modal view controller:

-(IBAction)goBack:(id)sender
{
    [(ParentViewController*)self.parentViewController setStringFromDefaults:newStringValue];
    [self dismissModalViewControllerAnimated:YES];
}

Where newStringValue is the updated string value that you have also stored in defaults.

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