gpt4 book ai didi

iphone - 在 UISplitViewController 中重用详细 View Controller

转载 作者:行者123 更新时间:2023-12-01 17:01:48 26 4
gpt4 key购买 nike

基本上,当我实现像 Apple 示例“MultipleDetailsViews”中所示的 Split View时,一切正常,每次选择一行时它都会分配一个新的详细 View 。以下是示例中的相关代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {    
UIViewController <SubstitutableDetailViewController> *detailViewController = nil;

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

// ...

NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
splitViewController.viewControllers = viewControllers;

// ...
}

但是我正在寻找的是重用详细的 View Controller ,即在选择 View Controller 时懒惰地分配它,并在我的对象中保留对它的引用。 That way when another row is selected the view controller is not deallocated and when it is selected again it would be reused instead of allocating a new one.以下是相关代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {    
UIViewController <SubstitutableDetailViewController> *detailViewController = nil;

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

// ...

NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
splitViewController.viewControllers = viewControllers;

// ...
}
self.firstDetailViewController第一次选择第一行时实例化,然后重复使用。

当我这样做时,它在横向模式下运行良好,但在弹出菜单中单击几下后在纵向模式下会引发异常: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Popovers cannot be presented from a view which does not have a window.'
那我为什么在乎?为什么我不想重新分配 View Controller ?因为在其中一些中,如果用户在新的详细 View 中导航而任务尚未完成,我希望执行不会被中断(杀死)的任务。

是否有人知道会发生什么或我正在努力实现的工作实现?

最佳答案

View Controller 旨在创建和丢弃,如果您需要在后台运行更长的时间,那么最好将其移动到主视图 Controller 或单独的对象中。

但是,如果您确实想尝试重用 View Controller ,这可以通过在新的导航 Controller 上设置 viewControllers 属性来实现,之前的细节 Controller 保存在 viewDidLoad 中:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
UINavigationController* navigationController = (UINavigationController*)[segue destinationViewController];

// reuse existing controller
navigationController.viewControllers = @[self.detailViewController];

// update the detail controller as normal.
[controller setDetailItem:object];
controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
controller.navigationItem.leftItemsSupplementBackButton = YES;
}
}

关于iphone - 在 UISplitViewController 中重用详细 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5963016/

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