iPhone - 通过 UITabBarItem 呈现 ModalViewController 并干净地解雇 ModalViewController

发布于 2024-10-12 00:17:57 字数 1746 浏览 4 评论 0原文

我有一个 tabBarController,通过将以下代码放入其中来添加:

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

当它实际呈现时,我认为这种方法在视觉上不吸引人,因为当我关闭模式视图时,我会回到一个空视图。

例如,许多条形码扫描应用程序或仅显示图像选择器的应用程序;做得相当成功。我只是想知道他们会使用什么样的实现来达到这样的效果。

这是一个名为 Path 的应用程序的屏幕截图,它具有与我想要的完全相同的功能:

 alt text

我还注意到,在这些应用程序中,如果您位于除中间选项卡栏项目之外的任何其他选项卡栏项目上,并且您单击呈现模式视图的选项卡栏项目,一旦它被关闭,它就不会实际上,它不会将它们带回一个空视图,它会像平常一样关闭,但是呈现模式视图的实际选项卡栏项目永远不会被选择。如果这是实现此类效果的唯一方法,我会对此类功能感到满意。

任何帮助将不胜感激,因为我已经陷入这个困境相当长一段时间了。另外,我什至不确定将所有这些代码放入我的 AppDelegate 中以便将视图控制器呈现为模式是否是正确的方法。这一切似乎都是错误的。

I have a tabBarController that I add by placing the following code into:

AppDelegate.h:

    ...
    UITabBarController IBOutlet *tabBarController;
}

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

AppDelegate.m:

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

I then use the following code to present a modal barcode scanning View Controller:

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

When it does actually get presented I think this method isn't visually appealing, because when I dismiss the modal view I am brought back to an empty view.

A lot of barcode scanning applications or applications that simply display an image picker for example; do this quite successfully. I'm just wondering what kind of implementation they would use in order to achieve such an effect.

This is a screenshot of an application called Path, which has the exact same functionality I'm after:

alt text

I also noticed that in these applications, if you are on any other tab bar item other than the middle one let's say, and you click on the tab bar item that presents the modal view, once it gets dismissed it doesn't actually bring them back to an empty view it dismisses like normal, however the actual tab bar item that presents the modal view is never selected. I would be happy with this type of functionality if that's the only way to implement this type of effect.

Any help would be greatly appreciated as I've been stuck in this for quite some time. Also I'm not even sure whether it's the right way to put all of this code in my AppDelegate in order for the View Controller to be presented as a modal. It all seems, just, wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苍暮颜 2024-10-19 00:17:57

不完全是我所追求的,但我认为我可以从此继续前进:

http://idevrecipes.com/2010/12/16/raised-center-tab-bar-button/

Not entirely what I'm after, but I think I can move forward from this:

http://idevrecipes.com/2010/12/16/raised-center-tab-bar-button/

随梦而飞# 2024-10-19 00:17:57

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

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

这段代码可能有错误,我只是输入它而没有测试。

When you dismiss the modal view controller, tell the tab bar to select whatever tab was originally selected.

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

You would have to save the original tab bar selection in a variable at the start of 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];
    }
}

There could be mistakes in this code, I just typed it without testing.

迷爱 2024-10-19 00:17:57

我通过使用 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];
    }
}

这将启动一个模式,该模式在关闭后会显示您在启动之前所在的相同选项卡。

I found a really easy solution by playing around UITabBarControllerDelegate--I only tried this in iOS 7 though.

First, subclass UITabBarController, make it its own UITabBarControllerDelegate, and create a property that'll hold a reference to the tab you want to launch a modal with. In my app, it's called the "Sell" tab.

@property (strong, nonatomic) UIViewController *sellTab;

Then, in your init method, just create that view controller and add it to the tabs.

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

Now here's where the magic is: override the following tab bar controller delegate methods. Code is pretty self-explanatory.

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

This will launch a modal which, upon dismissal, shows the same tab you were in before launch.

荆棘i 2024-10-19 00:17:57

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

您可以改为从选项卡之一呈现的视图中呈现模式视图。

或者,如果您只有一个主视图和想要以模态方式呈现的扫描视图,则应该使用一个按钮从主视图中呈现扫描视图。例如,您可以使用其中只有一个按钮的工具栏。

You shouldn't present a modal view, when the user clicks on a tab bar item.

You could instead present a modal view from within a view that's presented by one of the tabs.

Or, if you just have a single main view and the scan view you want to present modally, you should just use a button to present the scan view from within your main view. You could for instance use a toolbar with a single button in it, instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文