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

时间:2010-07-21 09:13:54

标签: iphone uisplitviewcontroller uiinterfaceorientation portrait

我在iPad上使用UISplitViewController for app。第一项任务是以纵向模式显示主视图和详细视图。我这样做了:

// 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];

好的,这样就行了,当我以纵向模式启动应用程序时,它会同时显示主视图和详细视图。

现在一切都有效。但我想在主视图中为每个记录(行)显示详细视图中的不同视图。 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仅显示没有masterView的detailView(整个屏幕)。

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

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

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

settingsSplitViewController = (SettingsSplitViewController *)self.splitViewController;

在方法didSelectRowAtIndexPath中,我这样做:

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

希望能帮助别人解决这类问题。