7

我有一个tabBarController,我通过将下面的代码添加:iPhone - 通过UITabBarItem和dismissModalViewController presentModalViewController干净

AppDelegate.h:

... 
    UITabBarController IBOutlet *tabBarController; 
} 

@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController; 

AppDelegate.m:

... 
    [self.window addSubview:tabBarController.view]; 
    [self.window makeKeyAndVisible]; 
    [tabBarController setDelegate:self]; 

然后我用下面的代码来呈现一个模式条形码扫描的视图控制器:

- (void)tabBarController:(UITabBarController *)tbc didSelectViewController:(UIViewController *)vc { 
     // Middle tab bar item in question. 
     if (vc == [tabBarController.viewControllers objectAtIndex:2]) { 
      ScanVC *scanView = [[ScanVC alloc] initWithNibName:@"ScanViewController" bundle:nil]; 

      // set properties of scanView's ivars, etc 

      UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:scanView]; 

      [tabBarController presentModalViewController:navigationController animated:YES]; 
      [navigationController release]; 
      [scanView release]; 
     } 
    } 

当它真正得到展示我觉得这个方法不够吸引人,因为当我关闭该模式的看法,我把回到空视图。

许多条码扫描应用程序或应用程序,例如简单地显示图像选择器;做得很成功。我只是想知道为了达到这样的效果他们会用什么样的实现。

这就是所谓的路径的应用程序的截图,其中有完全相同的功能,我以后:

alt text

我还注意到,在这些应用中,如果你在其他任何比中间的一个标签栏项目,我们可以说,并且您单击显示模式视图的标签栏项目,一旦它被解散它实际上不会将它们带回到它视为正常的空白视图,但实际选项卡显示模态视图的酒吧项目从未被选中。如果这是实现这种类型效果的唯一方法,我会对这种类型的功能感到满意。

任何帮助将不胜感激,因为我一直坚持了很长一段时间。此外,我甚至不确定是否将所有代码放入我的AppDelegate以便将View Controller作为模式呈现的正确方法。这一切似乎都是错的。

+1

我想你会发现Path应用程序使用自己的标签栏控制器实现。即不是来自Cocoa Touch的`UITabBarController` – ohhorob 2011-01-17 04:21:52

回答

1

当用户单击标签栏项目时,您不应该呈现模式视图。

您可以改为在其中一个选项卡提供的视图中显示模式视图。或者,如果您只有一个主视图和想要以模态方式呈现的扫描视图,则应该只使用一个按钮在主视图中显示扫描视图。相反,您可以使用带有单个按钮的工具栏。

+0

看看应用程序,例如Path和Stickybits,以获得我所追求的功能。 – fuzz 2011-01-16 03:11:31

+0

在我原来的问题中查看上面的截图。 – fuzz 2011-01-16 03:23:17

2

当您关闭模态视图控制器,告诉标签栏选择任何选项卡中最初选择。

- (void)dismissModalViewControllerAnimated:(BOOL)animated 
    { 
     // do whatever you need to do when dismissing 

     // savedTabIndex is an int ivar 
     // tabBarController is a reference, set when showing the modal view 
     [[self tabBarController] setSelectedIndex:savedTabIndex]; 
    } 

你将不得不保存在变量原来的标签栏选择在tabBarController:didSelectViewController:开始。

- (void)tabBarController:(UITabBarController *)tbc 
didSelectViewController:(UIViewController *)vc 
{ 
    // Save the tab bar index (if it's not the photo tab) 
    if ([tabBarController selectedIndex] != 3]) { 
     savedTabIndex = [tabBarController selectedIndex]; 
    } 
} 

这段代码可能存在错误,我只是在没有测试的情况下输入它。

1

我在UITabBarControllerDelegate附近发现了一个非常简单的解决方案 - 我只在iOS 7上试过。

首先,子类UITabBarController,使其成为它自己的UITabBarControllerDelegate,并创建一个属性,该属性将保存对要启动模式的选项卡的引用。在我的应用程序中,它被称为“销售”选项卡。

@property (strong, nonatomic) UIViewController *sellTab; 

然后,在你init方法,只需创建一个视图控制器,并把它添加到标签。

_sellTab = [[UIViewController alloc] init]; 
_sellTab.title = @"Sell"; 
self.viewControllers = @[homeTab, historyTab, _sellTab, bookmarksTab, profileTab]; 

现在这里是魔术的地方:覆盖下面的标签栏控制器委托方法。代码非常明了。

#pragma mark - Tab bar controller delegate 

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController 
{ 
    return viewController != self.sellTab; 
} 

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item 
{ 
    if (item == self.sellTab.tabBarItem) { 
     [self presentViewController:[[UINavigationController alloc] initWithRootViewController:[[PostAdViewController alloc] init]] animated:YES completion:nil]; 
    } 
} 

这将启动一个模式,解雇后显示您在启动之前的相同标签。