TabBar ( UITabBarController ) 中的 UISplitViewController ?
题
我所处的情况是,我需要从基于选项卡的应用程序开始,并且我需要一个或多个选项卡的拆分视图。但似乎分割视图控制器对象无法添加到 tabbarController 中。(尽管可以将 tabbar 对象添加到 splitviewcontroller 中)。
问题还可以从其他方面看:我的左侧有一个全屏,当在表中选择任何行时,我有一个表视图,弹出窗口应该指向该行。现在,当选择弹出窗口中的任何行时,该弹出窗口中的行将出现在所选行下方的左侧(只有该行可见),并且另一个弹出窗口会从所选行中出现。(面包屑导航类型)
我想我解释得很清楚了。那么大家有什么想法或解决方法吗?
如果我的问题不清楚,请告诉我。
谢谢,
马杜普
解决方案 3
我制成的示例应用程序。和发现,我们可以做到这一点编程,如:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSMutableArray *array = [NSMutableArray array];
NSMutableArray *tabArray = [NSMutableArray array];
UISplitViewController *splitViewConntroller = [[UISplitViewController alloc] init];
MainViewController *viewCont = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
[array addObject:viewCont];
[viewCont release];
viewCont = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
[array addObject:viewCont];
[viewCont release];
[splitViewConntroller setViewControllers:array];
[tabArray addObject:splitViewConntroller];
[splitViewConntroller release];
array = [NSMutableArray array];
splitViewConntroller = [[UISplitViewController alloc] init];
viewCont = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
[array addObject:viewCont];
[viewCont release];
viewCont = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
[array addObject:viewCont];
[viewCont release];
[splitViewConntroller setViewControllers:array];
[tabArray addObject:splitViewConntroller];
[splitViewConntroller release];
// Add the tab bar controller's current view as a subview of the window
[tabBarController setViewControllers:tabArray];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
return YES;
}
希望这有助于。
其他提示
使用界面生成器,创建分割视图控制器和标签栏控制器和它们链接到您的出口:
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@property (nonatomic, retain) IBOutlet UISplitViewController *splitViewController;
在应用程序委托didFinishLaunchingWithOption
,您的拆分视图控制器分配给标签栏控制器:
splitViewController.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"Title" image:nil tag:0] autorelease];
NSArray *controllers = [NSArray arrayWithObjects:splitViewController, /* other controllers go here */ nil];
tabBarController.viewControllers = controllers;
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
这将创建一个标签栏控制器(只有1在这种情况下标签),其在所有方向正确显示。
我已经写了为UISplitViewController将侦听更改设备的方向和定向自身相应的子类。有了这个类,我现在可以放置一个的UITabBarController内分裂的意见和每个拆分视图将正确的行为在旋转,即使它不是最前面的选项卡。我已经成功地在 TexLege 部署这一点,它被批准在App Store中使用,但您的里程可能会有所不同。请参阅在Github上库。
随意叉子和修改它,我总是希望听到关于它的评论(或投诉)。 https://github.com/grgcombs/IntelligentSplitViewController
要让tabbarcontroller显示为一个主视图splitviewcontroller你应该重写tabbarcontroller,这样它会支持或定向(所以说,使用类别的类的UITabBarController)
请参阅我关于将分割视图控制器改造为现有选项卡栏界面的文章: http://markivsblog.blogspot.com/2010/04/retrofitting-ipad-uisplitviewcontroller.html
我创建了一个子类的UITabBarController其正确传播旋转消息发送到它包含了所有UISplitViewControllers。这维持了UISplitViewControllers的正确的内部状态。然而,的SplitViewController代表方法之一不叫,如果SplitViewController是不可见的,所以在细节视图控制器viewWillAppear中方法考虑到这一点。我已经证实了这一工作在iOS5.0 - iOS6.1
<强> OSTabBarController.m 强>
#import "OSTabBarController.h"
@implementation OSTabBarController
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
for(UIViewController *targetController in self.viewControllers){
if(targetController != self.selectedViewController && [targetController isKindOfClass:[UISplitViewController class]]){
[targetController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
}
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
for(UIViewController *targetController in self.viewControllers){
if(targetController != self.selectedViewController && [targetController isKindOfClass:[UISplitViewController class]]){
[targetController didRotateFromInterfaceOrientation:fromInterfaceOrientation];
}
}
}
@end
<强> DetailViewController 强>
@implementation OSDetailViewController
-(void)viewWillAppear:(BOOL)animated{
//the splitViewController:willHideViewController:withBarButtonItem:forPopoverController: may not have been called
if(!UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){
self.navigationItem.leftBarButtonItem = nil;
}
}
#pragma mark - UISplitViewControllerDelegate Methods
- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
}
- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
}
@end
请即OS 3.2确实的不提供用于SPLITVIEW作为的TabBar视图适当的支持即可。
您可以把它的“工作”,但它有缺陷 - 最大的是,在另一个选项卡中的观点做出改变方向时往往会不会传播到SPLITVIEW标签观点正确,使视图去古怪,当你回去吧(左侧视图将占据整个屏幕,或barbutton物品遗失,等等)。
我已经到了,我要创造我自己的使用SPLITVIEW,因为这个问题一个tabBarController的结论。
我听说苹果正在对一个固定的传言,但它现在已经好几个月了,没有iPad的OS更新已经发生 - 也许是因为iPad的OS 4将解决这个问题
可以使用IB构建tabtab和修改标签来splitviewcontroller。
-(void) makeSplitViewController {
NSMutableArray *controllers = [NSMutableArray arrayWithArray:tabBarController.viewControllers];
int index = 0;
for (UIViewController *controller in tabBarController.viewControllers) {
if ([controller.tabBarItem.title isEqualToString:@"Stock"]) {
stockDetailController = [[StockDetailController alloc] initWithNibName:@"StockDetailController" bundle:nil];
stockMasterController = [[StockMasterController alloc] initWithStyle:UITableViewStylePlain];
stockMasterController.navigationItem.title = date;
stockMasterController.stockDetailController = stockDetailController;
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:stockMasterController] autorelease];
splitViewController = [[UISplitViewController alloc] init];
splitViewController.tabBarItem = controller.tabBarItem;
splitViewController.viewControllers = [NSArray arrayWithObjects:nav, stockDetailController, nil];
splitViewController.delegate = stockDetailController;
[controllers replaceObjectAtIndex:index withObject:splitViewController];
}
index++;
}
tabBarController.viewControllers = controllers;
}
我们成功地在 iOS5+ 的 iPad 上的 UITabViewController 中添加了 UISplitViewController。
使长话短说:有用:
- 如果您也接受纵向分割,则开箱即用;
- 有了一点工作,如果您想隐藏在肖像中的主视图,并且仅在点击按钮时才会出现。
第二种情况的技巧是使用IntelligentSplitViewController(参见一些帖子,感谢Greg Combs)或类似地扩展UISplitVC,并注意splitview控制器子类的委托始终是一个活动对象。
我们详细介绍了该过程: