【问题标题】:How can i add a custom delegate method to my singleton如何向我的单例添加自定义委托方法
【发布时间】:2014-08-10 15:05:26
【问题描述】:

我正在开发一个数字杂志阅读器应用程序,它需要先下载杂志。 在下载它们时,我想在视图控制器之间传递下载进度数据。 这就是我使用单例设计模式的原因。 我还使用 NSNotification 在下载时更新进度条百分比。但我认为每毫秒发送通知并不是很有效。所以我决定使用委托设计模式,但我不知道要实现自定义的 delagete 方法。对自定义委托有任何帮助吗?它是使用委托的最佳方式吗?

// Header

@interface ZXCSingleton : NSObject

+ (id)sharedInstance;

- (BOOL)isDownloadingProduct:(NSString *)productID;
- (void)addToDownloadListWithProductID:(NSString *)productID;
- (void)removeFromDownloadListWithProductID:(NSString *)productID;
- (NSArray *)getDownloadList;

- (void)setDownloadProgress:(float)progress
              withProductID:(NSString *)productID;
- (float)getDownloadProgressWithProductID:(NSString *)productID;

@end

// M

#import "ZXCSingleton.h"

@implementation ZXCSingleton{
    NSMutableArray *downloadList;
    NSMutableDictionary *downloadProgress;
}

+ (id)sharedInstance
{
    static ZXCSingleton *sharedInstance = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        sharedInstance = [[ZXCSingleton alloc] init];
    });
    return sharedInstance;
}

- (id)init
{
    self = [super init];
    if (self) {
        downloadList        = [[NSMutableArray alloc] init];
        downloadProgress    = [[NSMutableDictionary alloc] init];
    }
    return self;
}

- (BOOL)isDownloadingProduct:(NSString *)productID
{
    for (int i = 0; i < downloadList.count; i++) {
        if ([[downloadList objectAtIndex:i] isEqualToString:productID]) return YES;
    }
    return NO;
}

- (void)addToDownloadListWithProductID:(NSString *)productID
{
    [downloadList addObject:productID];
}

- (void)removeFromDownloadListWithProductID:(NSString *)productID
{
    [downloadList removeObject:productID];
}

- (NSArray *)getDownloadList
{
    return downloadList;
}

- (void)setDownloadProgress:(float)progress
              withProductID:(NSString *)productID
{
    if (progress != [[downloadProgress objectForKey:productID] floatValue]) [[NSNotificationCenter defaultCenter] postNotificationName:@"downloading" object:nil];
    [downloadProgress setObject:[NSString stringWithFormat:@"%0.2f", progress] forKey:productID];
}

- (float)getDownloadProgressWithProductID:(NSString *)productID
{
    return [[downloadProgress objectForKey:productID] floatValue];
}

【问题讨论】:

    标签: ios objective-c delegates singleton


    【解决方案1】:

    通过首先创建协议来定义委托,以定义委托所需的接口:

    @protocol ZXCSingletonProtocol 
    
       - (void)downloadProgessUpdates:(float)progress;
    
    @end
    

    在你的单例中为委托创建一个属性:

    @property (nonatomic, weak) id<ZXCSingletonProtocol> delegate; 
    

    让您的视图控制器符合该协议:

    @interface MyViewController : UIViewController <ZXCSingletonProtocol>
    

    并根据需要在视图控制器中实现downloadProgessUpdates(例如,使用给定的进度号更新 UI 元素)。请记住在初始化视图控制器时设置委托(viewDidLoad 会这样做):

    [ZXCSingleton sharedInstance].delegate = self; // self is the view controller instance
    

    在您的单例中更新 setDownloadProgress 方法以更新委托:

    [_delegate downloadProgessUpdates:progress];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-02
      • 2018-11-13
      • 1970-01-01
      • 1970-01-01
      • 2014-10-24
      相关资源
      最近更新 更多