djay
djay

Reputation: 375

How to make the views refresh between detail & master view controllers of UISplitViewController?

I have a split view controller in which either side has table views and needs table data reloading every time some interaction happens on either side. I have implemented delegate to update my detail view controller whenever a cell is selected on left side (master) of split view controller.

1.I wish to know do I need to implement another delegate to make it happen both ways (i.e. updating master view when a )or is there any generic approach.

2.I have already written code for both classes, so what is happening is that when i select a cell on left , right updates via a delegate method reloading/refreshing the view BUT the methods like viewWillAppear/viewDidAppear/viewWillDisapper ...etc are not called. I am now manually calling viewWillAppear method from the delegate Method that's triggered on left cell selection. Is there a technique i am missing so that the class methods are called automatically. or Can anyone point to the best approach to use a splitViewController?

Upvotes: 1

Views: 1555

Answers (4)

93sauu
93sauu

Reputation: 4127

Asnwering to your questions:

1º The protocol UISplitViewControllerDelegate has a method which said when splitviewcontroller is going to change the display mode. This method is very useful to update data of some view controller.

- (void)splitViewController:(UISplitViewController *)svc willChangeToDisplayMode:(UISplitViewControllerDisplayMode)displayMode;

2º For other hand, you can need to update the data of view controller when user selects a cell or any other actions.

If you share more information about your code I can be more specific.

Upvotes: 0

Koushik
Koushik

Reputation: 1250

Hi @Divjyot I am now working on similar scenario but I have to change to a different viewcontrolelr in detailVIewController(SecondaryViewController) on clicking a cell, so this is what I did I created and array with all viewcontrollers in masterViewController(PrimaryViewCOntroller) and passed to the detailViewController on cell selection using a delegate. So on clicking the cell in primaryViewControlelr updates the secondaryViewCOntroller with a new ViewController. If u want more info abt how to implement this comment below

Upvotes: 0

Mike M
Mike M

Reputation: 4436

First, it may not be a good idea to call viewWillAppear, etc from your code because those behaviors could change in the future. (e.g., viewDidLoad used to be called multiple times in the early iOS versions, now it is called once per instance). You could just move your code into a separate method.

Second, you may want to look at NSNotification as a way to communicate the changes. It's easy and doesn't require you to keep any delegate pointers around.

For example you might add to the child view controller .h:

#define MASTER_UPDATED  @"MasterUpdated"
#define DETAIL_UPDATED  @"DetailUpdated"

and then in the master controller something like:

[[NSNotificationCenter defaultCenter] addObserver:self 
   selector:@selector(updateFromDetail:) name:DETAIL_UPDATED object:nil];

...

-(void)updateFromDetail:(NSNotification *)no
{
    NSDictionary *nd = [no userInfo]; // get relevant information

    // take action....
}

From the detail side, when an item is tapped, you'd send a message like:

NSDictionary *userInfo = @{@"somekey":@"somevalue", @"anotherkey":@"anothervalue"};

[[NSNotificationCenter defaultCenter] postNotificationName:DETAIL_UPDATED 
    object:self userInfo:userInfo];

Upvotes: 2

fishinear
fishinear

Reputation: 6346

You probably need to use one of the reload... methods of UITableView to reload the table. The reloadData method will reload the complete table. If you know which rows are changed, then it is probably better to use one of the other methods. If the changes are also in the number of row, then you'll need to use one of the insert... or delete... methods to get proper animations. See the UITableView documentation for all the details.

For your first question, I would expect the detail view controller to have the master as a delegate. But the master should simply know which detail view controller is on the right. After all, he has started it.

Upvotes: 1

Related Questions