2010-04-07 9 views
0

est-il possible d'avoir deux contrôleurs de vue de détail pour un SplitViewController (pas avec l'utilisation d'Interface Builder, purement par programmation) ou pouvons-nous ajouter un splitviewcontroller à la place de detailviewcontroller? Si oui, veuillez m'expliquer avec un exemple.UISplitViewController avec deux DetailViewControllers

Merci à l'avance!

Répondre

2

Découvrez l'exemple de code this par Apple.

0

Bien sûr, vous pouvez utiliser plus d'un detailviewcontroller dans UISplitviewcontroller. Voir ce code.

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; 
} 

Dans 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]; 
} 

Dans chaque detailviewcontroller Ajouter ce délégué 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; 
} 

marque pragma - Support de rotation

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 

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

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

    } 
    } 
Questions connexes