gpt4 book ai didi

ipad - 从 RootController 更新 DetailViewController

转载 作者:行者123 更新时间:2023-12-02 11:02:48 27 4
gpt4 key购买 nike

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

  • RootView Controller ( TableView )位于 Split View的左侧,用于通过多 View 层次结构进行导航。当选择表格单元格时,新的表格 View 将被推到左侧
  • 左侧的新 View 可以更新详细信息 View 。

我可以完成这两项任务,但不能一起完成。我的意思是我可以在 RootController 中创建一个多级 TableView 。

或者我可以在 RootController 中创建一个单级 TableView ,它可以更新detailViewController。

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

链接中有更多源代码,但下面是我认为必须声明一个新的detailViewController(必须将其放入 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];
}
}

最佳答案

抱歉,我无法发布我的源代码,因为它包含敏感信息。当我有更多时间时,我将创建一个单独的项目并将代码上传到某个地方。

以下是我迄今为止的做法摘录(我欢迎任何反馈)。

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

#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 部分

将其添加到 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:

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

希望对你有帮助!

关于ipad - 从 RootController 更新 DetailViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3892283/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com