【问题标题】:Calling action sheet from tabbaritem in tabbar app在 tabbar 应用程序中从 tabbaritem 调用操作表
【发布时间】:2011-11-09 11:23:08
【问题描述】:

想法。

我正在开发一个基于 TabBarController 的应用程序。

有以下3个TabBarItem:

  • 主页(在导航控制器中显示主页视图)
  • 内容(在导航控制器中显示 contentView)
  • 分享(不像以前那样)

我希望 TabBarItem“共享”在当前视图中显示操作表,而不是更改视图。

我做了什么。

通过以下几行,我可以获得“共享”-TabBarItem 点击/触摸。

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {

if ([item.title isEqualToString:@"Share"]) {
    actionSheet = [[UIActionSheet alloc] initWithTitle:@"Share this App" delegate:self cancelButtonTitle:@"Cancle" destructiveButtonTitle:nil otherButtonTitles:@"Share on FaceBook", nil];
    actionSheet.actionSheetStyle = UIBarStyleBlackTranslucent;
    [actionSheet showInView:[UIApplication sharedApplication].keyWindow];
    [actionSheet release];
}

问题。

每次我选择“分享”时,都会显示一个空白视图。

我怎样才能阻止 TabBarItem 加载其视图,而只是导致显示操作表?

问候并感谢您考虑我的问题!

@Christopher A:感谢您的回复。

将以下代码放在我的“projectAppDelegate.m”中,但在选择 tabbaritem 时不会调用该方法。

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if ([viewController.title isEqualToString:@"Share"]) {
    return NO;
}
return YES;

【问题讨论】:

    标签: ios action tabbar


    【解决方案1】:

    tabBarController: shouldSelectViewController: 应该允许您以编程方式设置它。 查看它的详细信息here

    【讨论】:

      【解决方案2】:

      这是因为您在 [UIApplication sharedApplication].keyWindow 中显示工作表。

      试试这个:

      [sheet showInView:self.view];
      

      【讨论】:

      • 仍然在后台显示视图编辑:错误的视图;)
      【解决方案3】:

      不要使用 [UIApplication sharedApplication].keyWindow 来显示动作。

      将其替换为:

      [actionSheet showFromTabBar:self.tabBarController.tabBar];
      

      如果你没有得到你的 tabBarController 引用,那么首先获取你的 UItabbarcontroller 的引用。

      【讨论】:

        【解决方案4】:
         - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController: (UIViewController *)viewController
        {
        //    UIActionSheet *actionSheet = nil;
        switch (tabBarController.selectedIndex) {
            case 1:
            {
                NSLog(@"item 1");
                UIActionSheet *shareAS = [[UIActionSheet alloc]
                                              initWithTitle:nil
                                              delegate:self
                                              cancelButtonTitle:@"cancel"
                                              destructiveButtonTitle:nil
                                              otherButtonTitles:@"shareToXX", @"shareToYY",nil];
                shareAS.actionSheetStyle = UIActivityIndicatorViewStyleWhite;
                shareAS.tag = 1;
                [shareAS showInView:self.window];
            }
                break;
            case 2:
            {
                NSLog(@"item 2");
               UIActionSheet *myAS = [[UIActionSheet alloc]
                                              initWithTitle:nil
                                              delegate:self
                                              cancelButtonTitle:@"cancel"
                                              destructiveButtonTitle:nil
                                              otherButtonTitles:@"AAA", @"BBB",@"CCC",@"DDD",nil];
                myAS.actionSheetStyle = UIActionSheetStyleBlackOpaque;
                myAS.tag = 2;
                [myAS showInView:self.window];
            }
                break;
            default:
                break;
        }
        

        }

        希望能帮到你。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-08-23
          • 1970-01-01
          • 2011-07-06
          • 1970-01-01
          • 2011-07-22
          • 1970-01-01
          • 2013-01-24
          • 2011-11-14
          相关资源
          最近更新 更多