2

当用户在主视图中选择一个单元格时,我正在尝试加载一个特定UITableViewController的细节视图;UISplitViewController但有一些问题。当我选择单元格时,它只显示一个没有表格视图控制器的空白窗口。

但是,如果我将 App Delegate 更改为仅加载UITableView控制器而不使用UISplitViewController它,它工作正常,所以我知道这是我编码方式的问题UISplitViewController.

视图层次结构:

UISplitViewController 
--> 
    UINavigationController 
    --> UITableViewController (DetailViewController)

    UINavigationController
    --> UIViewController (ColorViewController)

用户选择一个单元格ColorViewController,这应该改变DetailViewController.

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

self.viewController = [[ViewController alloc] init];

self.window.rootViewController = [self.viewController splitViewController];

[self.window makeKeyAndVisible];

return YES;

}

ViewController.m

- (UIViewController *) splitViewController {

// Create the navigation-run root view
ColorViewController *rootVC = [ColorViewController controller];

UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:rootVC];

// Create the navigation-run detail view
DetailViewController *detailVC = [DetailViewController controller];

UINavigationController *detailNav = [[UINavigationController alloc] initWithRootViewController:detailVC];

// Add both to the split view controller
svc = [[UISplitViewController alloc] init];

svc.viewControllers = [NSArray arrayWithObjects: rootNav, detailNav, nil];

svc.delegate = detailVC;

return svc;

}

ColorViewController.m

@interface ColorViewController : UITableViewController

...    

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

UIViewController *controller = (UIViewController *)self.splitViewController.delegate;

TableViewController *tvc = [[TableViewController alloc] init];

[controller.view addSubview:tvc.view];

}

更改 Detail 视图的最佳方法是UISplitViewController什么?我应该将替换视图添加到UIViewController上面还是有更好的方法?也许这就是我的问题的原因?

4

2 回答 2

0

Since you have a navigation controller handling the detail side of your split view, your strategy should be to use it to push your new table view controller. When I've done this, I've kept references to both navigation controllers as properties in the app delegate so that I can use one or the other for push operations.

I'm not sure what the real purpose of your ViewController class is but if you move its splitViewController method into the app delegate it would be easier to change the navigation controllers from local variables to properties in a place where they can be easily accessed.

于 2012-11-27T12:31:50.930 回答
0

我要回答我自己的问题..

出于某种原因,在主视图(ColorViewController)中,细节对象TableViewController *tvc需要声明为实例变量,而不是方法中的变量。

之后,它可以正常工作并正确显示表格。

我真的不明白为什么,如果有人想解释一下。

干杯,

于 2012-11-27T12:45:33.093 回答