Master to Detail View - 返回主视图时重置详细视图

时间:2012-10-13 19:02:23

标签: ios6 master detailview

如何在主 - 详细信息视图类型应用中重置详细信息视图?例如,我的主视图只列出名称,当您选择名称时,我想要您选择的那个人的详细信息。应用程序将执行哪些操作。但是,当我从详细视图中按“后退”按钮并选择其他名称时,详细视图将保留第一个选定项目。如何重置该详细信息视图,以便显示所选内容的详细信息?

我在detailviewcontroller中有这个(Profile是一个类):

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

这是在我的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];
}

收到回答的答案

确定。 detailProfile是详细视图控制器的属性,我创建了一个方法:

@property (strong, nonatomic) Profile *detailProfile;
-(void)setDetailProfile:(Profile *)profile;

实施如下:

-(void)setDetailProfile:(Profile *)profile{
    NSLog(@"Going to display profile: %@, %@", [profile lastName], [profile firstName]);

    [firstName setText:[profile firstName]];
    [lastName setText:[profile lastName]];
}

现在无论出于何种原因,我的标签firstName和lastName都没有更新。

我在主人中做了以下事情:

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

选择新项目后,我的日志输出不会更新 - 从详细信息视图返回后 - 我的标签正在更新。

1 个答案:

答案 0 :(得分:0)

如果detailProfile不是详细信息视图控制器的属性,请将其转换为属性。创建自己的setDetailProfile方法,并在其中更新详细控制器(以及屏幕上)获取新配置文件时所需的任何内容。 (你现在没有说明你在做什么,但可能你可以将代码移出viewWillAppearviewDidAppear或其他东西。)

更改init方法,使其不需要profile参数,然后将master更改为:

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

编辑:

对于iPhone。因为没有拆分视图,请忽略有关创建自定义setDetailProfile方法的部分。相反,请将该属性设置为上方显示,但在self.detailProfile期间使用viewWillAppear:更新屏幕。