UISplitViewController有两个DetailViewControllers

时间:2010-04-07 10:35:18

标签: ipad

是否可以为SplitViewController提供两个详细的视图控制器(不使用Interface Builder,纯编程)或者我们可以添加一个splitviewcontroller来代替splitviewcontroller的detailviewcontroller。如果是,请举例说明。

提前致谢!

2 个答案:

答案 0 :(得分:2)

Apple查看this示例代码。

答案 1 :(得分:0)

当然,您可以在detailviewcontroller中使用多个UISplitviewcontroller。请参阅此代码。

<强> AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    self.rootVC = [[MasterViewController1 alloc]init];
    self.detailVC = [[DetailViewController alloc]init];
    UINavigationController *DetailNav = [[UINavigationController alloc]initWithRootViewController:self.detailVC];

    SplitVC = [[UISplitViewController alloc]init];
    SplitVC.viewControllers = [[NSArray alloc]initWithObjects:self.rootVC, DetailNav, nil];
    SplitVC.delegate = self.detailVC;

    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.window.rootViewController = SplitVC;
    [self.window makeKeyAndVisible];
    return YES;
}

在Masterviewcontroller.m

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = indexPath.row;
    [self.appDelegate.SplitVC viewWillDisappear:YES];
    NSMutableArray *viewControllerArray=[[NSMutableArray alloc] initWithArray:[[self.appDelegate.SplitVC.viewControllers objectAtIndex:1] viewControllers]];
    [viewControllerArray removeLastObject];


    if (row == 0) 
    {
        self.detailViewController = [[DetailViewController alloc] init];
        [viewControllerArray addObject:self.detailViewController];
        self.detailViewController.detailItem = self.detailViewController;
        self.appDelegate.SplitVC.delegate = self.detailViewController;
    }
    if (row == 1) 
    {
        self.currencyVC = [[CurrencyConvertorViewController alloc]init];
        [viewControllerArray addObject:self.currencyVC];
        self.currencyVC.detailItem = self.currencyVC;
        self.appDelegate.SplitVC.delegate = self.currencyVC;
    }

    if (row == 2) 
    {
        self.latest = [[PhoneExample alloc]init];
        [viewControllerArray addObject:self.latest];
        self.latest.detailItem = self.latest;
        self.appDelegate.SplitVC.delegate = self.latest;
    }

    if (row == 3) 
    {
        self.settingsVC = [[SettingPage alloc]init];
        [viewControllerArray addObject:self.settingsVC];
        self.settingsVC.detailItem = self.settingsVC;
        self.appDelegate.SplitVC.delegate = self.settingsVC;
    }
    [[self.appDelegate.SplitVC.viewControllers objectAtIndex:1] setViewControllers:viewControllerArray animated:NO];    
    [self.appDelegate.SplitVC viewWillAppear:YES];
}

在每个detailviewcontroller中添加此委托 UISplitViewControllerDelegate,UIPopoverControllerDelegate

<强> detailViewcontroller.m

-(void)viewDidLoad
{
   [super viewDidLoad];
   self.appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

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

    barButtonItem.title = @"Master";
    self.appDelegate.popoverController = pc;
    [[self navigationItem] setLeftBarButtonItem:barButtonItem];
    self.appDelegate.rootPopoverButtonItem = barButtonItem;

}


- (void)splitViewController: (UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem 
{
    [[self navigationItem] setLeftBarButtonItem:nil];
   self.appDelegate.popoverController = nil;
    self.appDelegate.rootPopoverButtonItem = barButtonItem;
}

pragma mark - 轮换支持

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    {
        [[self navigationItem] setLeftBarButtonItem:nil];

    }
    else 
    {
        [[self navigationItem] setLeftBarButtonItem:self.appDelegate.rootPopoverButtonItem];

    }   
  }