要求UISplitViewController延迟加载详细视图控制器的多个视图控制器 [英] Require UISplitViewController to lazy load multiple view controllers for detail view controller

查看:116
本文介绍了要求UISplitViewController延迟加载详细视图控制器的多个视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,可能不清楚标题。我已将此权利从 MultipleDetailView 中删除来自Apple的示例代码。每次用户从弹出窗口中的表中选择一行时,detailViewController都会再次分配FirstDetailViewController和SecondDetailViewController。我不想一遍又一遍地分配和初始化视图控制器,而是想在已选择行的情况下将详细信息分配给现有的和已经分配并初始化的视图控制器(如果存在于detailViewController中)。我修改了拆分视图模板而不是示例代码来实现我的需要。程序代码:

Well, might not be clear with the title. I have pulled this right out of the MultipleDetailView sample code from Apple. Every time the user selects a row from the table in the pop over, detailViewController is allocated the FirstDetailViewController and SecondDetailViewController again. Instead of allocating and initializing the view controller over and over, I want to assign the existing and already allocated and initialized view controller if existing to the detailViewController on the selection of the row. I have modified the Split View Template instead of the sample code to achieve what I need. Code from the program:

这是AppDelegate.h文件:

This is the AppDelegate.h file:

@interface iPadHelloWorldAppDelegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;

    UISplitViewController *splitViewController;

    MasterViewController *masterViewController;
    DetailViewController *detailViewController;
    SecondDetailViewController *secondDetailViewController;
}

这是AppDelegate.m文件:

This is the AppDelegate.m file:

 masterViewController = [[MasterViewController alloc] init];
 UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
 detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
 secondDetailViewController = [[SecondDetailViewController alloc] initWithNibName:@"SecondDetailView" bundle:nil];
 splitViewController = [[UISplitViewController alloc] init];
 splitViewController.viewControllers = [NSArray arrayWithObjects:navigationController, detailViewController, nil];
    splitViewController.delegate = detailViewController;
    // Add the split view controller's view to the window and display.
    [window addSubview:splitViewController.view];
    [window makeKeyAndVisible];

这是MasterViewController.m:

This is the MasterViewController.m:

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = indexPath.row;
    [self.appDelegate.splitViewController viewWillDisappear:YES];
    self.tempArrays = [NSMutableArray arrayWithArray:self.appDelegate.splitViewController.viewControllers];
    [self.tempArrays removeLastObject];
    if (row == 0) {
        [self.tempArrays addObject:self.appDelegate.detailViewController];
        self.appDelegate.splitViewController.delegate = self.appDelegate.detailViewController;
    }
    if (row == 1) {
        [self.tempArrays addObject:self.appDelegate.secondDetailViewController];
        self.appDelegate.splitViewController.delegate = self.appDelegate.secondDetailViewController;
    }
    self.appDelegate.splitViewController.viewControllers = self.tempArrays;
    [self.appDelegate.splitViewController viewWillAppear:YES];
}

这是DetailViewController.m:

This is the DetailViewController.m:

#pragma mark -
#pragma mark Split view support

- (void)splitViewController: (UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController: (UIPopoverController*)pc {

    barButtonItem.title = @"Master List";
    [navigationBar.topItem setLeftBarButtonItem:barButtonItem animated:NO];
    self.popoverController = pc;
}

// Called when the view is shown again in the split view, invalidating the button and popover controller.
- (void)splitViewController: (UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {

    [navigationBar.topItem setLeftBarButtonItem:nil animated:NO];
    self.popoverController = nil;
}

我可以延迟加载视图控制器,但是当我点击栏时弹出窗口按钮并跳转到第二个视图控制器,第二个视图控制器不显示弹出窗口。当我跳回第一个细节视图控制器时,会显示弹出窗口。

I am able to lazy load the view controllers, but when I tap the bar button for the popover and jump to the second view controller, the second view controller does not show the pop over. When I jump back to the first detail view controller, the popover is displayed.

基本上, here 是一个类似的问题。但是那里的投递箱的链接不起作用。

Basically, here is a similar question. But the link to the drop box there doesn't work.

推荐答案

因为你要我开枪 - 这里去了。从我们这里的意见来看,SplitViewController相当错误。如果您不完全按照Apple在示例代码中的方式执行操作,我们遇到了很多问题。

Since you have asked me to have shot - here it goes. The SplitViewController is rather buggy, from our opinion here. We have encountered many problems if you don't stick to exactly the way Apple does it in their sample code.

首先,我建议您采用示例代码再次从头开始,因为它似乎已经修改了很多。

First of all, I would suggest you take the sample code again and start from scratch, since it seems you have modified a lot.

至于你的问题:在你的委托和MainWindow.xib你设置你的SplitViewController。最重要的是不要像你那样设置viewControllers数组。

As for your problem: In your delegate and the MainWindow.xib you set up your SplitViewController. The most important thing is to not set up the viewControllers array the way you do it.

我遇到的问题是,如果我覆盖RootViewController,它会弄乱SplitViewController并产生类似你遇到的错误。

I encountered the problem that if I overwrite the RootViewController, it messes up the SplitViewController and produces bugs like the one you encounter.

尝试只设置一次RootViewController(TableViewController),并且永远不会在viewControllers属性中覆盖它。但是,对于DetailViewController,这似乎没问题。

Try setting up your RootViewController (the TableViewController) only once and never overwrite it in the viewControllers property. This seems to be OK for the DetailViewController, though.

其次,您的代码应该放在别处,而不是放在RootViewController中。这应该仅适用于tableView数据源和内容。

Secondly, you code should be placed elsewhere, not in the RootViewController. This should be for the tableView datasource and content only.

试试这个并在此反馈,我会尽快关注。

Try this and feedback here, I'll follow asap.

祝您好运。

编辑:代码添加 - 在您的RootViewController中执行此操作:

code addition - do this in your RootViewController:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {


DetailViewController *dvC = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

// take the original view controller from the splitviewcontroller as root
// appDelegateiPad defined like this in my appdelegate:
// #define appDelegateiPad ((AppDelegate_iPad *)[[UIApplication sharedApplication] delegate]) 
NSArray *viewControllers = [[NSArray alloc] initWithObjects:[[appDelegateiPad.splitViewController viewControllers]objectAtIndex:0], dvC, nil];
        appDelegateiPad.splitViewController.viewControllers = viewControllers;
//careful with this, set it to whatever your delegate is in your case           
appDelegateiPad.splitViewController.delegate = dvC;
        [viewControllers release];  


//this is my version
//i have the popoverController property in my detailviewcontroller. this is where my splitviewcontroller delegate methods are. you need to set the popovercontroller property in the class where your splitviewcontroller delegate methos are
    dvC.popoverController = [[[appDelegateiPad.splitViewController viewControllers]objectAtIndex:1] popoverController];     

    } 
}

这篇关于要求UISplitViewController延迟加载详细视图控制器的多个视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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