views:

168

answers:

3

I have a RootViewController class and a UserSettingsController class. I have defined the UITableView methods (numberOfRowsInSection, cellForRowAtIndexPath) in the RootViewController class.

I want to reload the table view defined in the RootViewController class from the UserSettingsController class. How can I get control of the tableView Object in the RootViewController class from the UserSettingsController class?

I tried the following, but it tries to load a new tableview object.

RootViewController *rootViewController = [[RootViewController alloc]init]; 
[rootViewController.mytableView reloadData];
[rootViewController autorelease];
A: 

I am presuming if what you want to do is to show the same exact table in two different view controllers.

Make myTableView a global or singleton and share it as a property between rootViewController and userSettingsController. Better yet, do a little more work and create a class for your myTableView so you can more easily set it up and manipulate it without having to spread duplicate code in the view controllers' implementation.

In each view controller, test to see if (myTableVIew == nil), and if so, go ahead and initialize and set it up (preferably by going through the tableview class you made). Once set up, you need to make sure you add myTableView to the properties (retained) of both view controllers. Then to display this tableView in each of the controllers, you will just have to do a [self.view addSubview:myTableView]; where self is the view controller that is active at the time.

mahboudz
I think he wants to just call reloadData of RootViewController rather than showing the same tableView in other views.
Chintan Patel
A: 

You can reload rootViewController.mytableView in viewWillAppear method of RootViewController itself. This will make rootViewController.mytableView reload when you are about to go to the rootViewController view. If the data you want to load is not much (as in takes more time to load like fetching data from the web) you will be fine with this solution.

Otherwise, to load rootViewController.mytableView from you settings view, you can use NSNotification like this:

In RootViewController.m :

//This goes in viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTableViewData) name:@"ReloadRootViewControllerTable" object:nil];

Then make a method like this:
-(void) reloadTableViewData{

   [mytableView reloadData];
}

In Settings view, where you want to reload the RootViewController tableView, write this:

[[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadRootViewControllerTable" object:nil];

This will automatically call the reloadTableViewData method of RootViewController without your need to do subclassing or anything. :)

Make use of notifications with custom name to call static methods in other classes. They are very handy.

Chintan Patel
Chintan: Thanks for this suggestion. I also researched on asynchronous events and notifications and the strategy worked. I also tested another way where we store the table object in app delegate and use reload method on that table object from any other class. That also worked for me.
Iya
Thank you for accepting my answer. Yes, taking objects in app delegate will always work but then i would discourage you to take one more global variable. If your program becomes sufficiently large, you will end up with about 80-100 variables in app delegate which will make it difficult to maintain, debug or expand. So i would suggest keeping tableView for RootViewController in RootViewController itself rather than globalizing it.
Chintan Patel
A: 

Yes mahboudz you are absolutely correctly saying that tableview valuw ould be nil

Because if reinitialize the class from another class using init , the objects are reset to nil which are previously set.

And than when you want to reload it it doesn't works because the value of tableview is nil

I didnot get better answer for resolving this please let me know if any buddy knows the solution

crystal