How do I reset the detail view in a master-detail view type app? For example, my master view simply lists names and when you select on a name, I want the details of that person you selected. Which the application will do. However, when I press the "back" button from the detail view and select another name, the detail view stays with the first selected item. How do I reset that detail view so it will show the details of whatever is selected?
I have this in my detailviewcontroller (Profile is a class):
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil title:(NSString *)name details:(Profile *)profile{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"Detail", @"Detail");
NSLog(@"Details: %@", [profile lastName]);
_detailProfile = profile;
}
return self;
}
And this is in my masterviewcontroller:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.detailViewController) {
//self.detailViewController = [[MediaDirectoryDetailViewController alloc] initWithNibName:@"MediaDirectoryDetailViewController" bundle:nil];
_showProfile = [_profileArray objectAtIndex:indexPath.row];
self.detailViewController = [[MediaDirectoryDetailViewController alloc] initWithNibName:@"MediaDirectoryDetailViewController" bundle:nil title:@"Detail" details:_showProfile];
}
NSDate *object = _objects[indexPath.row];
self.detailViewController.detailItem = object;
[self.navigationController pushViewController:self.detailViewController animated:YES];
}
EDITED FROM THE ANSWER RECEIVED
Ok. detailProfile is a property of the detail view controller and I created a method:
@property (strong, nonatomic) Profile *detailProfile;
-(void)setDetailProfile:(Profile *)profile;
Implemented as follows:
-(void)setDetailProfile:(Profile *)profile{
NSLog(@"Going to display profile: %@, %@", [profile lastName], [profile firstName]);
[firstName setText:[profile firstName]];
[lastName setText:[profile lastName]];
}
Which, now for whatever reason, my labels firstName and lastName are not being updated.
I did the following in the master:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.detailViewController) {
//self.detailViewController = [[MediaDirectoryDetailViewController alloc] initWithNibName:@"MediaDirectoryDetailViewController" bundle:nil];
_showProfile = [_profileArray objectAtIndex:indexPath.row];
NSLog(@"Profile selected last name: %@", [_showProfile lastName]);
// self.detailViewController = [[MediaDirectoryDetailViewController alloc] initWithNibName:@"MediaDirectoryDetailViewController" bundle:nil title:@"Detail" details:_showProfile];
self.detailViewController = [[MediaDirectoryDetailViewController alloc] initWithNibName:@"MediaDirectoryDetailViewController" bundle:nil title:@"Detail"];
self.detailViewController.detailProfile = _showProfile;
}
NSDate *object = _objects[indexPath.row];
self.detailViewController.detailItem = object;
[self.navigationController pushViewController:self.detailViewController animated:YES];
}
My outputs to the log don't update when a new items is selected - after going back from the detail view - and my labels are updating.
If detailProfile
is not a property of the detail view controller, turn it into a property. Create your own setDetailProfile
method and, in there, update anything in your detail controller (and on screen) that you need to when you get a new profile. (You don't say where you're doing that currently but probably you can move the code out of viewWillAppear
or viewDidAppear
or something.)
Change your init method so that it doesn't need a profile parameter and then change your master to something like:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.detailViewController) {
self.detailViewController = [[MediaDirectoryDetailViewController alloc] initWithNibName:@"MediaDirectoryDetailViewController" bundle:nil title:@"Detail"];
}
_showProfile = [_profileArray objectAtIndex:indexPath.row];
self.detailViewController.detailProfile = _showProfile;
NSDate *object = _objects[indexPath.row];
self.detailViewController.detailItem = object;
[self.navigationController pushViewController:self.detailViewController animated:YES];
}
EDIT:
For an iPhone. because there's no split view, ignore the part about creating a custom setDetailProfile
method. Instead, set the property as show above, but then use self.detailProfile
to update the screen during viewWillAppear:
.