2010-04-07 46 views
0

是否有可能爲SplitViewController(不是使用Interface Builder,純粹使用編程)提供兩個詳細視圖控制器,或者我們可以添加一個splitviewcontroller來代替splitviewcontroller的detailviewcontroller。如果是的話,請用一個例子來解釋我。有兩個DetailViewControllers的UISplitViewController

在此先感謝!

回答

2

檢出this Apple的示例代碼。

0

當然,您可以在UISplitviewcontroller中使用多個detailviewcontroller。看到這個代碼。

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

編譯馬克 - 旋轉支持

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 

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

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

    } 
    } 
相關問題