UISplitView - 为 masterView 中的每一行加载不同的 detailView

Posted

技术标签:

【中文标题】UISplitView - 为 masterView 中的每一行加载不同的 detailView【英文标题】:UISplitView - load different detailView's for each row in masterView 【发布时间】:2010-07-21 09:13:53 【问题描述】:

我在 iPad 上为应用程序使用 UISplitViewController。第一个任务是以纵向模式显示主视图和详细视图。我是这样做的:

// It is possible to keep the Master View in portrait mode
// also. Just pass YES to this method to enable this mode.
- (id) initWithMasterInPortraitMode:(BOOL) masterInPortrait 
    self = [super init];
    self.keepMasterInPortraitMode = masterInPortrait;
    return self;


// Thanks to http://intensedebate.com/profiles/fgrios for this code snippet
-(void) viewWillAppear:(BOOL)animated 
    NSLog(@"viewWillAppear");
    if(keepMasterInPortraitMode == NO) 
        return;
    

    //check interface orientation at first view and adjust it
    //if it is in portrait mode
    if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
        UIViewController* master = [self.viewControllers objectAtIndex:0];
        UIViewController* detail = [self.viewControllers objectAtIndex:1];
        [self setupPortraitMode:master detail:detail];
       


// Thanks to http://intensedebate.com/profiles/fgrios for this code snippet
- (void)setupPortraitMode:(UIViewController*)master detail:(UIViewController*)detail 
    //adjust master view
    CGRect f = master.view.frame;
    f.size.width = 320;
    f.size.height = 1024;
    f.origin.x = 0;
    f.origin.y = 0;

    [master.view setFrame:f];

    //adjust detail view
    f = detail.view.frame;
    f.size.width = 448;
    f.size.height = 1024;
    f.origin.x = 321;
    f.origin.y = 0;

    [detail.view setFrame:f];

我这样创建 splitView:

MySplitViewController *mySplitViewController = [[MySplitViewController alloc] initWithMasterInPortraitMode:YES];

好的,这没问题,当我以纵向模式启动应用程序时,它会并排显示主视图和详细视图。

现在一切正常。但我想在主视图中为每条记录(行)在 detailView 中显示不同的视图。 我在 masterView 中的 didSelectRowAtIndexPath 方法如下所示:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    if (indexPath.row == 0) 
        FirstDetailViewController *firstDetailView = [[FirstDetailViewController alloc] initWithNibName:@"firstDetailView" bundle:nil];
        UINavigationController *firstDetailNavigationController = [[UINavigationController alloc] initWithRootViewController:firstDetailView];
        [firstDetailView release];

        [self.splitViewController setViewControllers:[NSArray arrayWithObjects:self.navigationController, firstDetailNavigationController, nil]];
    
    else 
        SecondDetailViewController *secondDetailView = [[SecondDetailViewController alloc] initWithNibName:@"secondDetailView" bundle:nil];
        UINavigationController *secondDetailNavigationController = [[UINavigationController alloc] initWithRootViewController:secondDetailView];
        [secondDetailView release];

        [self.splitViewController setViewControllers:[NSArray arrayWithObjects:self.navigationController, secondDetailNavigationController, nil]];
    

在第一行或第二行(在 masterView 中)单击(触摸)后,splitView 仅显示 detailView(整个屏幕)而没有 masterView。

如何强制 splitView 在更改视图、主视图和详细视图时显示???

感谢您的帮助!

【问题讨论】:

我从 iPhone 开发者中心找到了一个例子——MultipleDetailsView,但我不明白协议:developer.apple.com/iphone/library/samplecode/… 有人知道如何实现吗? 【参考方案1】:

我找到了解决方案。在 masterView 我创建了 splitViewController 实例并将本地实例分配给该实例:

settingsSplitViewController = (SettingsSplitViewController *)self.splitViewController;

在方法 didSelectRowAtIndexPath 我是这样做的:

[settingsSplitViewController setupPortraitMode:self.navigationController detail:detailNavigationController];

希望它能帮助其他人解决此类问题。

【讨论】:

以上是关于UISplitView - 为 masterView 中的每一行加载不同的 detailView的主要内容,如果未能解决你的问题,请参考以下文章

UISplitView,在设置代表时接收语义问题

更改 UISplitView 中的 RootViewController

从 UISplitView 呈现全屏模式视图?

通用应用程序的 iPad UISplitView 问题

如何对 UISplitView 进行动画切换?

UISplitview:从 appDelegate 访问 UITabBarController