iphone - 从 TabBar 调用 ActionSheet

标签 iphone objective-c ios uitabbar uiactionsheet

我有一个带有 4 个按钮(主页、访问、共享、更多)的标签栏。我希望我的选项卡栏中的“共享”按钮调用一个操作表,该操作表具有适当的按钮来选择我希望用户能够分发指向应用程序的链接(Facebook、Twitter、电子邮件等)的特定 channel 。

我可以使用 Storyboard 来执行此操作,但我必须创建一个唯一的(空白) View Controller ,该 View Controller 链接到选项卡栏中的“共享”按钮。我将以下代码放在 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];

虽然这可行,但从用户的角度来看并不是最佳选择。我希望操作表显示在选择“共享”按钮时显示的特定 View 上。例如,如果用户在“访问” View 上并从选项卡栏中选择“共享”,我希望操作表显示在访问 View 的顶部,并且该 View 在操作表后面可见。

我可以使用 Storyboard 实现吗?或者我是否需要自定义标签栏才能实现此功能?如果需要自定义标签栏,我将不胜感激关于如何实现它的一些指导/见解,包括自定义标签栏以及来自标签栏的操作表。

提前致谢!

最佳答案

我也需要这个,四处搜索,最终在 UITabBarController 上构建了以下类别。

让操作表出现在当前选项卡上的诀窍是您永远不想实际访问该操作表呈现选项卡。用启动操作表的 UIButton 覆盖该位置的选项卡栏。

像往常在 Storyboard中那样绘制标签栏,在特殊行为发生的位置留下一个空的栏项。然后添加这个类别...

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

关于iphone - 从 TabBar 调用 ActionSheet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13943809/

相关文章:

ios - Objective-C 对象放置 iPad

ios - 我需要在收到通知后使用 Firebase 调用 API(应用程序处于打开状态、后台状态、事件状态)

javascript - ng-repeat 在 iOS 中不起作用

iphone - 那里有什么好的 chipmunk/cocos2d 示例源吗?

ios - 选择表格 View 中的 ScrollView 时禁用表格 View 的滚动

iphone - CoreAnimation CALayer 和 CATextLayer 组合

ios - UITableViewCell 细节指标飞来飞去

iphone - 景观中的 UIRefreshControl

objective-c - MFMessageComposeViewController 未正确显示

ios - 如何在应用程序内浏览器中打开在我的 iOS 应用程序中单击的任何 URL?