目前我正在開發一個應用程序,它使用四種協議在類之間進行通信。三個工作正常,但仍然沒有工作。我已將其設置爲與其他人相同,但委託人始終丟失其ID。我對Objective-C非常陌生,所以我不能深究它的底部。下面是我做的:iOS - 單身人士沒有按照委託人的想法工作
我有委託
@property (weak, nonatomic) id <PlayerProtocol> player;
和MainViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
[[Interface sharedInstance] Init];
NSLog(@"Player ID: %@", _player);
NSLog(@"viewDidLoad: %@", self);
}
- (void)sedPlayer:(id) pointer{ //sed is no typo!
_player = pointer;
NSLog(@"sedPlayer ID: %@", _player);
NSLog(@"sedPlayer: %@", self);
}
+ (instancetype)sharedInstance {
static dispatch_once_t once;
static id sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
在Interface.m(NSObject的)
- (void)Init {
[[MainViewController sharedInstance] sedPlayer:self];
}
一個MainViewController.h
當然還有一個protocol.h,但這並不重要,因爲代表會遇到麻煩!當我運行的代碼我得到的控制檯上的輸出如下:
sedPlayer ID: <Interface: 0x1700ab2e0>
sedPlayer: <MainViewController: 0x100406e30>
Player ID: (null)
viewDidLoad: <MainViewController: 0x100409550>
所以很明顯的是,單不工作作爲MainViewcontroller的情況是不同的。對於單身人士,我使用dispatch_once標準方法,因爲我可以使用其他正常工作的協議。 ARC打開。有沒有人有線索這裏有什麼問題,爲什麼單身人士不工作?
首先你應該使用'時使用'delegate'你,assign'更換strong'的':'@property(強,非原子)id player',其次,請在您的問題中發佈您的整個代碼,我們可以找到問題出在哪裏。 –
aircraft
^不,使用'weak'而不是'assign'。 'assign'將會要求你在釋放對象的時候不去引用那個對象,否則當你發送一條消息給(現在是釋放的)對象時你會崩潰。 – AdamPro13
@Hecot是否確定'[MainViewController sharedInstance]'是'MainViewController'的同一個實例,因爲'viewDidLoad'被調用? – AdamPro13