从RootController更新DetailViewController [英] Updating DetailViewController from RootController

查看:111
本文介绍了从RootController更新DetailViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用与Apple的Mail应用程序类似的用户界面创建一个iPad应用程序,即:

I'm trying to create an iPad application with a similar user interface to Apple's Mail application, i.e:


  • RootView控制器(表格)视图)在拆分视图的左侧,用于具有多视图层次结构的导航。当选择表格单元格时,左侧会按下新的表格视图。

  • 左侧的新视图可以更新详细视图。

我可以完成这两项任务但不能完成任务。
我的意思是我可以在RootController中创建一个多级表视图。(这里你可以找到工作源代码)。

I can accomplish both tasks BUT NOT TOGETHER. I mean I can make a multi-level table view in the RootController.(HERE you can find the working source code).

或者我可以在RootController中创建一个单级表视图,它可以更新detailViewController(这里有源代码) :http://www.megaupload.com/?d = D6L0463G)。

Or I can make a single-level table view in the RootController which can update the detailViewController (here there is the source code:http://www.megaupload.com/?d=D6L0463G).

有谁能告诉我如何在RootController中制作一个可以更新的多级表一个detailViewController?

Can anyone tell me how to make a multi-level table in the RootController which can update a detailViewController?

链接上有更多的源代码,但下面是我假设我必须声明一个新的detailViewController的方法(必须放入UISplitViewController):

There is more source code at the link but below is the method in which I presume I have to declare a new detailViewController (which has to be put in the UISplitViewController):

- (void)tableView:(UITableView *)TableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];

//Get the children of the present item.
NSArray *Children = [dictionary objectForKey:@"Children"];

//
if([Children count] == 0) {
    /*
    Create and configure a new detail view controller appropriate for the selection.
    */
    NSUInteger row = indexPath.row;
    UIViewController <SubstitutableDetailViewController> *detailViewController = nil;

    if (row == 0) {
        FirstDetailViewController *newDetailViewController = [[FirstDetailViewController alloc]initWithNibName:@"FirstDetailView" bundle:nil];
        detailViewController = newDetailViewController;
    }

    if (row == 1) {
        SecondDetailViewController *newDetailViewController = [[SecondDetailViewController alloc]initWithNibName:@"SecondDetailView" bundle:nil];
        detailViewController = newDetailViewController;
    }

    // Update the split view controller's view controllers array.
    NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
    splitViewController.viewControllers = viewControllers//nothing happens.....
    [viewControllers release];// 
}

else {

    //Prepare to tableview.
    RootViewController *rvController = [[RootViewController alloc]initWithNibName:@"RootViewController" bundle:[NSBundle mainBundle]];

    //Increment the Current View
    rvController.current_level += 1;

    //Set the title;
    rvController.current_title = [dictionary objectForKey:@"Title"];

    //Push the new table view on the stack
    [self.navigationController pushViewController:rvController animated:YES];

    rvController.tableDataSource = Children;
    [rvController.tableView reloadData]; //without this instrucion,items won't be loaded inside the second level of the table 

    [rvController release];
    }
}


推荐答案

抱歉,但我不能发布我的源代码,因为它包含敏感信息。当我有更多时间可用时,我将创建一个单独的项目并在某处上传代码。

Sorry, but I cannot post my source code as it contains sensitive information. When I have more time available I will create a separate project and upload the code somewhere.

以下是我到目前为止所做的工作的摘录(欢迎任何反馈)。

Here are extracts of how I have done it so far (I welcome any feedback).

RootViewController - 注意我的根表中有4个部分。

The RootViewController - Note I have 4 sections in my root table.

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    // Detail view logic
    NSUInteger section = indexPath.section;
    UIViewController <SubstitutableDetailViewController> *detailViewController = nil;

    if (section == 2) {
        ProductSearchDetailView *viewController = [[ProductSearchDetailView alloc] initWithNibName:@"ProductSearchDetailView" bundle:nil];
        detailViewController = viewController;
        //[viewController release];
    }
    else {
        DetailViewController *defaultDetailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
        detailViewController = defaultDetailViewController;
        //[defaultDetailViewController release];
    }

    // Navigation logic 
    switch (section) {
        case 0: 
        {
            break;
        }
        case 1:
        {
            break;
        }
        case 2:
        {
            // new Navigation view
            ProductSearchViewController *viewController = [[ProductSearchViewController alloc] initWithNibName:@"ProductSearchViewController" bundle:nil];
            viewController.navigationItem.backBarButtonItem.title = @"Back";
            [self.navigationController pushViewController:viewController animated:YES];
            [viewController release];

            break;  
        }
        case 3:
        {
            StoreLocatorNavController *viewController = [[StoreLocatorNavController alloc] initWithNibName:@"StoreLocatorNavController" bundle:nil];
            viewController.navigationItem.backBarButtonItem.title = @"Back";
            [self.navigationController pushViewController:viewController animated:YES];
            [viewController release];
            break;
        }   
    }

    // Update the split view controller's view controllers array.
    NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
    splitViewController.viewControllers = viewControllers;
    [viewControllers release];

    // Dismiss the popover if it's present.
    if (popoverController != nil) {
        [popoverController dismissPopoverAnimated:YES];
    }

    // Configure the new view controller's popover button (after the view has been displayed and its toolbar/navigation bar has been created).
    if (rootPopoverButtonItem != nil) {
        [detailViewController showRootPopoverButtonItem:self.rootPopoverButtonItem];
    }

    [detailViewController release];

}

NSNotificationCenter部分

NSNotificationCenter part

将此添加到ProductSearchViewController:

Add this to ProductSearchViewController:

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSDictionary *itemAtIndex = (NSDictionary *)[self.productResults objectAtIndex:indexPath.row];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"updateProduct" object:itemAtIndex];
}

最后,将其添加到ProductSearchDetailViewController:

And finally, add this to ProductSearchDetailViewController:

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTheProductDetails:) name:@"updateProduct" object:nil];

}

- (void)updateTheProductDetails:(NSNotification *)notification {
NSDictionary *productDictionary = [NSDictionary dictionaryWithDictionary:[notification object]];

    // product name
    _productName.text = [productDictionary objectForKey:@"ProductDescription"];
}

希望有所帮助!

这篇关于从RootController更新DetailViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆