我正在开发一个 iOS 应用程序。我使用的设计模式单例和委托(delegate)模式。我在 dealloc 方法中没有委托(delegate)对象。如果用户重新打开此屏幕并设置为委托(delegate)对象自身。但我看到委托(delegate)对象总是为零。如何重新分配给 self 来委托(delegate)对象
最佳答案
由于您的问题不详细,
我想给你建议,首先仔细检查你的单例代码,然后
检查您的委托(delegate)是否始终以正确的方式按照您的逻辑正确设置。
我知道这个答案太宽泛和笼统,但是,这是没有帮助的。
作为引用,下面的代码是 ARC 下 Objective-C 单例的基本结构。(对于 swift 版本,请检查上面的答案。)
你的Singleton.h
@interface YourSingleton : NSObject
+ (YourSingleton*)sharedInstance;
@end
你的Singleton.m
#import "YourSingleton.h"
@implementation YourSingleton
#pragma mark - singleton method
+ (YourSingleton*)sharedInstance
{
static dispatch_once_t predicate = 0;
__strong static id sharedObject = nil;
dispatch_once(&predicate, ^{
sharedObject = [[self alloc] init];
});
return sharedObject;
}
@end
关于iOS 委托(delegate)和单例模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21582458/