简体   繁体   English

从UITabBar按钮调用的iPhone UIActionSheet

[英]iPhone UIActionSheet called from UITabBar Button

Is there a way to call an ActionSheet directly from a tabbar. 有没有一种方法可以直接从标签栏调用ActionSheet。 I'm working on a program where the user wants a contact button on the tab bar that displays an actionsheet with the appropriate buttons. 我正在开发一个程序,用户希望标签栏上的联系人按钮显示带有相应按钮的操作表。

Thanks in advance 提前致谢

您是否只能让一个视图控制器与这些选项卡之一相关联,然后将其视图保留为空白,然后在viewDidLoad中创建actionSheet?

I agree with all the people above saying that this isn't a good idea (and that this should be done using a toolbar), but it's definitely doable. 我同意上述所有人的观点,这不是一个好主意(应该使用工具栏来完成),但这绝对是可行的。

The code below implements one of the UITabBarControllerDelegate methods, and avoids the selection of the tab bar item, and instead creates and displays a UIActionSheet: 下面的代码实现UITabBarControllerDelegate方法之一,并避免选择选项卡栏项目,而是创建并显示UIActionSheet:

-   (BOOL)tabBarController:(UITabBarController *)tabBarController 
shouldSelectViewController:(UIViewController *)viewController
{
    NSInteger choice = 0; // --> index of the view controller that should "act as button"
    if (viewController == [tabBarController.viewControllers objectAtIndex:choice])
    {
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil 
                                                           delegate:nil 
                                                  cancelButtonTitle:@"Cancel" 
                                             destructiveButtonTitle:nil
                                                  otherButtonTitles:@"Send e-mail", @"Twitter", @"Whatever", nil];
        [sheet showFromTabBar:tabBarController.tabBar];
        return NO;
    }
    return YES;
}

例如,邮件应用程序是您正在查看电子邮件,然后单击弯曲的箭头按钮以调出“转发-答复”操作表。

I think I can understand what Steve's after. 我想我能理解史蒂夫的追求。 I have a main activity reached via the 1st element in the TabBarController. 我通过TabBarController中的第一个元素达到了主要活动。 I'd like the 2nd tab-bar element to selectively add either a modalview email, an access to facebook, or an access to twitter. 我希望第二个标签栏元素有选择地添加modalview电子邮件,对facebook的访问或对twitter的访问。 It would be nice if that choice is offered via an actionsheet so as to not lose sight of what's "behind" from that first view controller (from the first tab-bar choice) and THEN the new view controller handling the choice shows up. 如果通过操作表提供该选择会很好,以免看不到该第一个视图控制器(从第一个选项卡栏选择)的“背后”,然后出现处理该选择的新视图控制器。 This seems to be what "AP mobile" does when you want to 'share' a news article, for example. 例如,当您要“共享”新闻文章时,这似乎就是“ AP移动设备”的作用。

@Adrian : I couldn't get your solution to work out-of-the-box.. but then found out why (read on...) @Adrian:我无法让您的解决方案开箱即用。.但后来找出了原因(请继续阅读...)

It doesn't help (obviously) to specifically drag from the Outlet:delegate to file-owner You'll get: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Changing the delegate of a tab bar managed by a tab bar controller is not allowed.' 特别是从Outlet:delegate拖动到文件所有者没有帮助(显然),您将得到:***由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'更改由...管理的选项卡栏的委托标签栏控制器是不允许的。”

My delegate method was being ignored until I added the UITabBarControllerDelegate to the interface definition (UIApplicationDelegate was already present and I didn't read further)... AND In applicationDidFinishLaunching I added 在将UITabBarControllerDelegate添加到接口定义之前,我的委托方法被忽略了(UIApplicationDelegate已经存在,并且我没有进一步阅读)...并且在applicationDidFinishLaunching中添加了

[rootController setDelegate:self];

Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM