使用 UINavigationController 从另一个控制器更新或重新加载 UIViewController?

发布于 2024-12-03 14:37:46 字数 1753 浏览 0 评论 0原文

我有一个 iOS 应用程序,它有一个 mainControllerView,在那里我创建了一个 UINavigationController,然后我将一个名为 FirstViewController 的新 UIViewController 和其他视图推入该 UINavController。

简而言之,是这样的:

MainController(UIVIewController) 
   |  
    --> UINavController 
         | 
          --> FirstViewController(UIViewController)
         |
          --> SecondViewController(UIViewController)
         |
          --> AddViewController

另外,在 FirstViewController 内部我有一个 UITableView,该 UITableView 是由 CoreData 提供的。

现在,我决定在右上角(导航栏)添加一个“加号”系统按钮,以将另一行添加到数据库中,因此应该反映到 UITableview 中。

我所做的是创建另一个自定义视图,称为 AddViewController,当我按下“加号”按钮时,它会被推入 NavController 堆栈。在该视图中,我只是添加了一些文本字段和一个按钮来更新数据库,这是有效的,但是,当我按回键时,我找不到更新位于 FirstViewController 内部的 UITableView 的方法。

简而言之,

UINavController(with FirstViewController)
 1.-When I press the "+" button another UIViewController (AddViewContronller) 
    is pushed
 2.-In that view I present a few UITextfields to be filled.
 3.-I press a "save" button on the NavigationBar, that button save info 
    into the DB and then it goes back the FirstViewController, popping the current
    UIViewController(AddViewController)
 4.-FirstViewController and its tableView is on the main screen again, however
    the rows are still the same.

当我按下 AddViewController 中的保存按钮时,我一直在尝试更新 FirstViewController,但正如我所说,我没有成功。

我没有尝试过的选项是当我跳转到另一个 UIViewController 时释放 FirstViewController,然后在后面重新分配,但我不认为这是一个好主意,因为使用 NavigationController 我可以跟踪所有进入的视图控制器堆栈。

所以有人知道我应该怎么做才能从另一个 UIViewController 刷新/重新加载视图(UIViewController),在我的例子中从 AddViewController 更新 FirstViewController 视图?

另外,我想听听我是否走在正确的道路上?或者我想做的事情可以用另一种方法(例如模式对话框)轻松完成?

任何帮助都会非常感激

, 劳尔

I have an iOS app that it has a mainControllerView, there I create a UINavigationController and then I push into that UINavController a new UIViewController called FirstViewController, and other views.

In short is something like this:

MainController(UIVIewController) 
   |  
    --> UINavController 
         | 
          --> FirstViewController(UIViewController)
         |
          --> SecondViewController(UIViewController)
         |
          --> AddViewController

Also, inside the FirstViewController I have a UITableView, that UITableView is being fed by CoreData.

Now, I decided to add a "plus" system button on the top-right corner (of the NavigationBar) to add another row into the DB and therefore that should be reflected into the UITableview.

What I did was create another custom view, called AddViewController which is pushed into the NavController stack when I press that "plus" button. In that View, I just added a few textfields and a button to update the DB, that is working, however, I cannot find a way to update the UITableView, which is inside of FirstViewController, when I press back.

Again in short,

UINavController(with FirstViewController)
 1.-When I press the "+" button another UIViewController (AddViewContronller) 
    is pushed
 2.-In that view I present a few UITextfields to be filled.
 3.-I press a "save" button on the NavigationBar, that button save info 
    into the DB and then it goes back the FirstViewController, popping the current
    UIViewController(AddViewController)
 4.-FirstViewController and its tableView is on the main screen again, however
    the rows are still the same.

I've been trying to update the FirstViewController when I press the save button in AddViewController, but as I said, I've been unsuccessful.

The option I have not tried is release the FirstViewController when I'm jumping to another UIViewController and then reallocate in the back, but I don't think that it's a good idea as using the NavigationController I can track all my view controllers that are into the stack.

So anybody knows what should I do to refresh/reload a View (UIViewController) from another UIViewController, in my case update FirstViewController view from AddViewController?

Also, I would like to heard if I am in the right track? or what I want to do it can be easily done with another approach like a modal dialog?

Any help it will be very much appreciate

cheers,
Raul

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

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

发布评论

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

评论(2

记忆里有你的影子 2024-12-10 14:37:46

您可以尝试处理 NSManagedObjectContextObjectsDidChangeNotification 通知。例如:

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(dataDidChange)
                                                 name:NSManagedObjectContextObjectsDidChangeNotification
                                               object:nil];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:NSManagedObjectContextObjectsDidChangeNotification
                                                  object:nil];
}

- (void)dataDidChange {
    [self.tableView reloadData];
}

You can try to handle the NSManagedObjectContextObjectsDidChangeNotification notification. For example:

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(dataDidChange)
                                                 name:NSManagedObjectContextObjectsDidChangeNotification
                                               object:nil];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:NSManagedObjectContextObjectsDidChangeNotification
                                                  object:nil];
}

- (void)dataDidChange {
    [self.tableView reloadData];
}
昇り龍 2024-12-10 14:37:46

首先,如果您不熟悉UITableView,您应该阅读iOS 表格视图编程指南

如果您使用 Core Data 来填充 UITableView,那么您几乎总是应该使用 NSFetchedResultsController

First of all, if you are not familiar with UITableView, you should read Table View Programming Guide for iOS.

And if you use Core Data to populate your UITableView, you almost always should use NSFetchedResultsController.

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