2014-02-05 29 views
2

我正在開發一個iOS應用程序。我用過的設計模式Singleton和Delegation模式。我在dealloc方法中沒有使用委託對象。如果用戶重新打開此屏幕並設置爲委託對象自我。但我看到委託對象始終爲零。我怎樣才能重新分配到自我的委託對象iOS代理和單例模式

+3

從描述中診斷這是不可能的。請張貼一些代碼。 – dasblinkenlight

+0

你在哪裏重新分配你的委託給自己? –

+0

其實我的問題委託對象不重新分配?我想重新分配給委託對象 – hiwordls

回答

0

使用此在您的Singleton類方法

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

你應該問的問題更簡單。您可以找到有關大多數設計模式的信息,以及如何在ios here上實施它們。 希望這有助於。

2

由於您的問題不詳細, 我想給你建議要仔細檢查你的單身代碼第一和 檢查是否不是你的代理設置正確隨着始終以正確的方式你的邏輯。
我知道這個過於寬泛和普遍的答案,但它不能被幫助。

作爲參考,這下面代碼是ARC下Objective-C的單的基本結構。(用於迅速版本,檢查上述的答案。)

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