ShareKit:为不同的共享者自定义文本(SHKActionSheet)

时间:2012-12-30 18:13:46

标签: objective-c subclass sharekit

根据ver.2中的official FAQ根据用户选择的分享者自定义文本/内容,您需要:

    来自SHKActionSheet的
  1. 子类并覆盖 dismissWithClickedButtonIndex
  2. 在中设置新的子类名称 配置器(将其返回(Class)SHKActionSheetSubclass;)。
  3. 它对我不起作用。但更重要的是:我把

    NSLog(@"%@", NSStringFromSelector(_cmd));
    

    在(类)SHKActionSheetSubclass中查看它是否被调用。并且它不是;((所以ShareKit不关心这个配置选项... 有人之前有过这个吗?

    谢谢你!

    UPD1:我在这里放了一些代码。 这是我的子类ITPShareKitActionSheet的样子。根据文档,我需要覆盖dismissWithClickedButtonIndex:animated:,但要跟踪我的类是否被调用,我也会覆盖actionSheetForItem:

    + (ITPShareKitActionSheet *)actionSheetForItem:(SHKItem *)item
    {
        NSLog(@"%@", NSStringFromSelector(_cmd));
    
        ITPShareKitActionSheet *as = (ITPShareKitActionSheet *)[super actionSheetForItem:item];
    
        return as;
    }
    
    - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animate
    {
        NSLog(@"%@", NSStringFromSelector(_cmd));
    
        NSString *sharersName = [self buttonTitleAtIndex:buttonIndex];
        [self changeItemForService:sharersName];
        [super dismissWithClickedButtonIndex:buttonIndex animated:animate];
    }
    

    以下是我在代码中用来创建操作表时用户按下“共享”按钮:

    - (IBAction)shareButtonPressed:(id)sender
    {
        // Create the item to share
        SHKItem *item = [SHKItem text:@"test share text"];
    
        // Get the ShareKit action sheet
        ITPShareKitActionSheet *actionSheet = [ITPShareKitActionSheet actionSheetForItem:item];
    
        // Display the action sheet
        [actionSheet showInView:self.view]; // showFromToolbar:self.navigationController.toolbar];
    }
    

    当我运行此代码时,按“共享”按钮并选择我期望在日志中获得两行的任何共享者:

    1. actionSheetForItem: - 已创建自定义操作表
    2. dismissWithClickedButtonIndex:animated: - 自定义机制 调用了进程操作表的按下按钮。
    3. 但出于某种原因,我只记录了第一行。

2 个答案:

答案 0 :(得分:0)

编辑:目前为止,最好的方法是使用SHKShareItemDelegate。更多信息在ShareKit's FAQ

答案 1 :(得分:0)

我遇到了同样的问题,但我突然得到它成功调用我的子类。

首先,我的配置器设置如下:

-(Class) SHKActionSheetSubclass{
    return NSClassFromString(@"TBRSHKActionSheet");
}

现在我的子类: .h文件

    #import "SHKActionSheet.h"
    @interface TBRSHKActionSheet : SHKActionSheet

    @end

.m实现覆盖:

#import "TBRSHKActionSheet.h"
#import "SHKActionSheet.h"
#import "SHKShareMenu.h"
#import "SHK.h"
#import "SHKConfiguration.h"
#import "SHKSharer.h"
#import "SHKShareItemDelegate.h"

@implementation TBRSHKActionSheet

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
if (self) {
    // Initialization code
}
return self;
}

    + (SHKActionSheet *)actionSheetForItem:(SHKItem *)i
{
        NSLog(@"%@", NSStringFromSelector(_cmd));

    SHKActionSheet *as = [self actionSheetForType:i.shareType];
    as.item = i;
    return as;
}

     - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
{
   NSInteger numberOfSharers = (NSInteger) [sharers count];

    // Sharers
    if (buttonIndex >= 0 && buttonIndex < numberOfSharers)
    {
        bool doShare = YES;
        SHKSharer* sharer = [[NSClassFromString([sharers objectAtIndex:buttonIndex]) alloc] init];
        [sharer loadItem:item];
        if (shareDelegate != nil && [shareDelegate respondsToSelector:@selector(aboutToShareItem:withSharer:)])
        {
            doShare = [shareDelegate aboutToShareItem:item withSharer:sharer];
        }
        if(doShare)
            [sharer share];
    }

// More
else if ([SHKCONFIG(showActionSheetMoreButton) boolValue] && buttonIndex == numberOfSharers)
{
    SHKShareMenu *shareMenu = [[SHKCONFIG(SHKShareMenuSubclass) alloc] initWithStyle:UITableViewStyleGrouped];
    shareMenu.shareDelegate = shareDelegate;
    shareMenu.item = item;
    [[SHK currentHelper] showViewController:shareMenu];

}

[super dismissWithClickedButtonIndex:buttonIndex animated:animated];
}

最后在我的实现文件中,我没有像Vilem所建议的那样修改对SHKActionSheet的调用,因为有些依赖似乎会给我带来冲突。

所以这是我的来电者(直接来自教程):

            NSURL *url = [NSURL URLWithString:@"http://getsharekit.com"];
    SHKItem *item = [SHKItem URL:url title:@"ShareKit is Awesome!" contentType:SHKURLContentTypeWebpage];

    // Get the ShareKit action sheet

    SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];

    // ShareKit detects top view controller (the one intended to present ShareKit UI) automatically,
    // but sometimes it may not find one. To be safe, set it explicitly
    [SHK setRootViewController:self];

    // Display the action sheet
    [actionSheet showFromToolbar:self.navigationController.toolbar];

这对我没有任何问题。