从推送的 viewController 到 rootViewController 的信息

发布于 2024-10-04 05:49:48 字数 551 浏览 5 评论 0原文

一个简单的问题。我在导航控制器中有一个表格视图。当我触摸某个单元格时,它会使用该单元格中的信息推送视图控制器,以便我可以在新视图中对其进行编辑。现在工作正常了(我们可以将其称为信息路径:“rootviewcontroller -> Pushed viewcontroller”)。但是当我在新视图中单击“保存”时,我希望在调用 popviewcontroller (信息路径:“pushed viewcontroller -> rootviewcontroller”)之前将编辑的值返回到 rootviewcontroller,以便编辑的值可以显示在表视图中。

正确的做法是什么?

编辑:

pushViewController 和 popViewController 正在工作。我只要求在调用“保存”按钮(popViewController)时将编辑的信息返回到 rootViewController 以在表视图中显示的最佳方法。我想我只需要直接从推送的 viewController 中使用新信息编辑 pList 即可。尽管我更愿意将新信息发送到 rootViewController 并让它处理对 pList 文件的访问。

Sort of a simple question. I have a tableview in a navigationcontroller. When I touch a cell, it pushes a view controller with the information from the cell, so I can edit it in the new view. Now thats working correct (we can call it the informationpath: "rootviewcontroller -> pushed viewcontroller"). But when I click save in the new view, I want the edited values to travel back to the rootviewcontroller before I call popviewcontroller (informationpath: "pushed viewcontroller -> rootviewcontroller"), so the edited values can be displayed in the tableview.

Whats the correct approach to this?

EDIT:

pushViewController and popViewController is working. I only asked for the best approach to get the edited information back to the rootViewController for display in the tableview, when Save-button (popViewController) was called. I guess I'll just have to edit the pList with the new information directly from the pushed viewController. Though I would prefer sending the new information to the rootViewController and have it handle the access to the pList-file.

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

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

发布评论

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

评论(4

孤单情人 2024-10-11 05:49:48

我有同样的情况 - 两个表格视图。第一个 TV 显示数据库记录列表,当点击其中一个时,它会进入第二个 TableView,显示记录的详细信息。我通过将详细信息 TableViewController 推送到导航控制器堆栈来实现此目的。到目前为止就这么简单。

我遇到的问题是,在更新详细信息表视图(控制器)中的记录后,我想让列表表视图控制器知道,以便它可以更新记录列表。

我做的第一件事是向详细信息表视图控制器添加一个属性,以便当在记录列表上选择一行时,列表控制器可以将核心数据托管实体传递到详细信息控制器。

同时,我还添加了列表控制器作为核心数据更改事件的观察者,如下所示:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataSaved:) name:NSManagedObjectContextDidSaveNotification object:nil];

因此,如果详细信息表视图和控制器更新记录,则列表控制器将调用其 dataSaved: 方法,传递一个 NSNotification 对象。

在 dataSaved: 方法中,我检查对象,如果正在编辑的核心数据实体位于更新的列表中,则我设置一个标志来表示需要更新。但是,如果插入的列表中有一条记录,则意味着已创建一条新记录并将其插入数据库,因此设置一个标志来触发列表表视图的完全重新加载。

当用户返回到列表视图控制器时,会触发 viewDidAppear: 方法。在此方法中,我检查标志,

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

如果记录已更新,则调用重新加载特定记录,或者如果插入新记录,则告诉表视图进行完全重新加载。

最后,我将列表控制器作为核心数据通知的观察者删除,因为它不再感兴趣。

我不知道这是否是推荐的方法,但到目前为止它对我有用。

I have the same situation - two tableviews. The first TV displays a list of database records and when one is tapped it goes through to the second TableView which displays the details of the record. I do this by pushing the details TableViewController onto the navigation controllers stack. So far so go and quite simple.

The problem I encountered was that after updating the record in the details table view (controller), I wanted to let the list table view controller know, so that it could update the list of records.

The first thing I did was to add a property to the details table view controller so that when a row was selected on the list of records, the list controller could pass the core data managed entity to the details controller.

At the same time, I also added the list controller as a observer of core data change events like this:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataSaved:) name:NSManagedObjectContextDidSaveNotification object:nil];

So if the details table view and controller update the record, the list controller has it's dataSaved: method called, passing a NSNotification object.

In the dataSaved: method I examine the object and if the core data entity being edited is in the updated list, then I set a flag to signal an update is required. However if there is a record in the inserted list, it means that a new record has been created and inserted into the database, so a flag is set to trigger a full reload of the list table view.

When the user returns to the list view controller, the viewDidAppear: method is triggered. In this method I examine the flags and either call

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

to reload the specific record if the record was updated, or tell the table view to do a full reload if there is an insert of a new record.

Finally I then remove the list controller as an observer of core data notifications because it is no longer interested.

I don't know if this is the recommended way to do this, but so far it's working for me.

偏闹i 2024-10-11 05:49:48

您可以尝试在 popViewController 之前重新加载数据,但您应该发布一些代码。

You can try to reloadData before popViewController but you should post some code.

梦言归人 2024-10-11 05:49:48

所有 UIViewController 都有一个 navigationController 方法。当您将视图控制器推送到导航控制器时,该属性将设置为指向导航控制器。

因此,只需在视图控制器中调用 [self.navigationController popViewControllerAnimated:YES]; 即可。

另外,不要混淆视图和视图控制器。当您在视图中单击“保存”时,应确保“保存”按钮调用视图控制器上的方法,该方法又与导航控制器通信。

All UIViewControllers have a navigationController method. When you push a view controller to a navigation controller, that property is set to point to the navigation controller.

So, just call [self.navigationController popViewControllerAnimated:YES]; in the view controller.

Also, don't confuse views and view controllers. When you click save in the view, you should make sure that the save button calls a method on your view controller, which in turn talks to the navigation controller.

小瓶盖 2024-10-11 05:49:48

点击一行后,您将进入包含适当信息的新视图。

您可以在此处执行一些操作,例如编辑数据。
因此,首先单击“保存”按钮保存更改(通过调用 IBAction),在此 IBAction 方法中将检查是否(更改已成功保存),然后调用

[self.navigationController popViewControllerAnimated:YES];

并确保表通过更新的数据库重新加载自身(为此通过在 viewWillAppear 方法中调用数据库方法来更改数据数组)。

On tapping a row You are going to new view with appropriate information.

Here you perform some operation such as editing data.
So first save the changes on click on save button (By calling an IBAction) and in this IBAction method will check wheter (changes are successfully saved) then call

[self.navigationController popViewControllerAnimated:YES];

And ensure that table reload itself by updated databese (for this change array of data by calling database method in viewWillAppear method).

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