0

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

我的 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;
}

这是在我的主视图控制器中:

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

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

4

1 回答 1

0

如果detailProfile不是详细视图控制器的属性,则将其转换为属性。创建您自己的setDetailProfile方法,并在其中更新您的详细控制器(和屏幕上)中的任何内容,当您获得新的配置文件时您需要这样做。(你没有说你目前在哪里做,但可能你可以将代码移出viewWillAppearviewDidAppear或其他东西。)

更改您的 init 方法,使其不需要配置文件参数,然后将您的 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.detailProfileviewWillAppear:.

于 2012-10-13T19:17:49.520 回答