2

I am developing an iOS application. My used design pattern Singleton and Delegation patterns. I did nil a delegate object in dealloc method. If user re opened this screen and set to delegate object self. But I see delegate object always nil. How can I re assign to self to delegate object

hiwordls
  • 781
  • 7
  • 17
  • 35

3 Answers3

2

Since your question is not detailed, I would like to give you suggestion to double-check your singleton code first and check whether of not your delegate is properly set along with your logic in the right way always.
I know this is too broad and general answer, However, it cannot be helped.

For reference, This Below code is Basic structure of Objective-C singleton under ARC.(for swift version, check the above answers.)

YourSingleton.h

@interface YourSingleton : NSObject

+ (YourSingleton*)sharedInstance;

@end

YourSingleton.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
boraseoksoon
  • 2,164
  • 1
  • 20
  • 25
0

Use this in your singleton Class method

//Strict impelmentation of singleton with ARC.
//Object is created only once in the System.
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    if(myObject==nil){
        myObject= [[myClass alloc]init];
    }
});

return sharedEffcoreObject;
Prince Agrawal
  • 3,619
  • 3
  • 26
  • 41
0

You should ask the question more briefly. You can find information about most of design patterns, and how to implement them on ios here. Hope this helps.

csk
  • 418
  • 1
  • 5
  • 11