开发者

Calling action sheet from tabbaritem in tabbar app

The idea.

I'm developing a TabBarController based app.

There are the following 3 TabBarItems:

  • Home (shows homeView within a navigation controller)
  • Content (shows contentView within a navigation controller)
  • Share (doesn't do what it's used to)

I'd like the TabBarItem "Share" to show an Action Sheet at the current view, not to change the view.

What I've done.

With the following lines I'm able to get the "Share开发者_开发技巧"-TabBarItem Click/Touch.

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

The problem.

Every time I select "Share", a blank view is shown.

How can I stop the TabBarItem from loading its view, just causing the Action Sheet being shown?

Greets and thank you for thinking about my problem!

@Christopher A: thanks for your response.

Placed the following code in my "projectAppDelegate.m" but the method isn't called when selecting a tabbaritem.

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


tabBarController: shouldSelectViewController: should allow you to programmatically set this. Check out the details of it here.


This is because you are showing sheet in [UIApplication sharedApplication].keyWindow.

Try this one:

[sheet showInView:self.view];


Don't use [UIApplication sharedApplication].keyWindow to show action.

Replace it with :

[actionSheet showFromTabBar:self.tabBarController.tabBar];

If you are not getting your tabBarController references then first get the reference of your UItabbarcontroller.


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

}

hope to help you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜