【问题标题】:Master to Detail View - Resetting the detail view when going back to the master view主到详细视图 - 返回主视图时重置详细视图
【发布时间】:2012-10-13 19:02:22
【问题描述】:

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

我的 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 都没有更新。

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

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

【问题讨论】:

    标签: ios6 master detailview


    【解决方案1】:

    如果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 方法的部分。相反,如上所示设置属性,然后在viewWillAppear: 期间使用self.detailProfile 更新屏幕。

    【讨论】:

    • 我已经按照你的建议做了,但是现在,无论出于何种原因,我在视图中的标签都没有更新,即使 NSLog 显示应该在那里。有任何想法吗?我已经编辑了我的原始帖子。
    • 我的错误...我一直在做 iPad 编程,但错过了你在 iPhone 上的事实。 (不同的是,详细控制器在 iPad 上只显示一次。)不要更新setDetailProfile 中的名称,只需保存配置文件信息并将viewWillAppear 中的更新为[firstName setText:[self.detailProfile firstName]]; 等。实际上,您甚至不需要 setDetailProfile 的自定义版本。
    • 我已经清理了很多东西,似乎我的 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 在从详细视图。我在这个函数的日志中输出了一个输出,它只在你第一次在主控中选择一个项目时输出。一旦您从详细信息中选择“返回”并在主列表中选择另一个项目,它就永远不会输出下一个项目。
    猜你喜欢
    • 2016-05-14
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多