4

4つのボタン([ホーム]、[訪問]、[共有]、[その他])のあるタブバーがあります。タブバーの[共有]ボタンで、ユーザーがアプリ(Facebook、Twitter、Eメールなど)へのリンクを配布できるようにする特定のチャネルを選択するための適切なボタンを含むアクションシートを呼び出すようにします。

ストーリーボードを使用してこれを行うことはできますが、タブバーの[共有]ボタンにリンクされている一意の(空白の)ビューコントローラーを作成する必要があります。「共有」ボタンが選択されたときにアクションシートを表示するために、viewDidAppearメソッド内に次のコードを配置しました。

int selectedTab = self.tabBarController.selectedIndex;

if (selectedTab == 2)
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Share Your Visit with Friends!" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Facebook", @"Twitter", @"Email eduLaunchpad", nil];

    [actionSheet showFromTabBar:self.tabBarController.tabBar];
}

アクションシートを閉じると、デフォルトでホームビューに戻ります。

[self.tabBarController setSelectedIndex:0];

これは機能しますが、ユーザーの観点からは最適ではありません。「共有」ボタンが選択されたときに表示されていた特定のビューよりも、アクションシートを表示したいと思います。たとえば、ユーザーが[訪問]ビューを表示していて、タブバーから[共有]を選択した場合、アクションシートを訪問ビューの上部に表示し、そのビューをアクションシートの後ろに表示します。

Storyboardを使用してこれを実現できますか?または、この機能を実装するためにカスタムタブバーが必要ですか?カスタムタブバーが必要な場合は、カスタムタブバーやタブバーのアクションシートなど、それを実装する方法についてのガイダンス/洞察をいただければ幸いです。

前もって感謝します!

4

2 に答える 2

1

私もこれが必要で、ググって、最終的にUITabBarControllerに次のカテゴリを作成しました。

アクションシートを現在のタブに表示する秘訣は、実際にそのアクションシートを表示するタブにアクセスしたくないということです。その位置にあるタブバーを、アクションシートを開始するUIButtonで覆います。

ストーリーボードで通常行うようにタブバーをペイントし、特別な動作が発生する位置に空のバーアイテムを残します。次に、このカテゴリを追加します...

//  UITabBarController+Button.h

@interface UITabBarController (Button) <UIActionSheetDelegate>
- (void)replaceItemAtIndex:(NSUInteger)index withButtonImage:(UIImage*)buttonImage;
@end

//  UITabBarController+Button.m

#import "UITabBarController+Button.h"

#define kBUTTON_TAG   4096

@implementation UITabBarController (Button)

- (void)replaceItemAtIndex:(NSUInteger)index withButtonImage:(UIImage*)buttonImage {

    UIButton *button = (UIButton *)[self.view viewWithTag:kBUTTON_TAG];

    if (!button) {
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = kBUTTON_TAG;
        [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

        UITabBar *bar = self.tabBar;
        CGFloat width = bar.frame.size.width / bar.items.count;
        button.frame = CGRectMake(index*width, bar.frame.origin.y, width, bar.frame.size.height);

        [self.view addSubview:button];
    }
}

#pragma mark - Handle button tap

- (void)buttonTapped:(id)sender {

    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Action Sheet:"
                                        delegate:self
                               cancelButtonTitle:@"Cancel"
                          destructiveButtonTitle:nil
                               otherButtonTitles:@"Action A", @"Action B", @"Action C", nil];

    [sheet showFromTabBar:self.tabBar];
}

インデックス<tabBar.items.countとボタンの画像を使用して呼び出します。アプリのルートVCの場合は、次のように呼び出すことができます。

#import "the category i suggest above"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // whatever else you do in here, then
    NSLog(@"%@", self.window.rootViewController);  // make sure this is a UITabBarController
    // if it is, then ...
    UITabBarController *tbc = (UITabBarController *)self.window.rootViewController;
    [tbc replaceItemAtIndex:2 withButtonImage:[UIImage imageNamed:@"some image name"]];

    return YES;
}
于 2012-12-19T03:31:01.447 に答える
0

タブバーからアクションシートを開く代わりに

[actionSheet showFromTabBar:self.tabBarController.tabBar];

このコードを使用してください。

[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
于 2013-04-26T12:10:33.243 に答える