我有一個UIViewController
這是不響應NSNotifications
。我確信通知所啓動的行動是有效的。我的設置有什麼問題?UIViewController沒有響應NSNotifications
我使用一個singleton從AppDelegate
的didFinishLaunching
加載feedViewController
。此feedViewController
保留對deckViewController
的引用,並且此deckViewController
可靠地發送feedViewController
應該遵守的通知。但通知從未收到。我確信singleton
對應該接收通知的視圖控制器保持強有力的參考。
在保存於該通知觀察者的引用singleton
:
// .h file
@property (nonatomic) ROFeedViewController *feedViewController;
// .m file
_feedViewController = [[ROFeedViewController alloc] init];
IIViewDeckController *deckController = [[IIViewDeckController alloc] initWithCenterViewController:_feedViewController leftViewController:leftController rightViewController:rightController];
_feedViewController.deckController = deckController;
在通知觀察者(ROFeedViewControler
):
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewDeckPanned:) name:@"ViewDeckPanned" object:_deckController];
...
}
- (void)viewDeckPanned:(NSNotification *)notification {
float offset = [(NSNumber *)[notification object] floatValue];
if (offset > 0) {
_leftButton.transform = CGAffineTransformMakeRotation(offset * M_PI/180.0);
} else {
_rightButton.transform = CGAffineTransformMakeRotation(offset * M_PI/180.0);
}
}
通知可靠獲得從通知發送者(IIViewDeckController
)發送:
- (void)notifyDidChangeOffset:(CGFloat)offset orientation:(IIViewDeckOffsetOrientation)orientation panning:(BOOL)panning {
[self performDelegate:@selector(viewDeckController:didChangeOffset:orientation:panning:) offset:offset orientation:orientation panning:panning];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ViewDeckPanned" object:[NSNumber numberWithFloat:offset]];
}