开发者

updating or reloading a UIViewController from another Controller using UINavigationController?

开发者 https://www.devze.com 2023-04-03 15:15 出处:网络
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

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 righ开发者_如何学JAVAt 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


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];
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消