我有一个UIViewController
没有响应的NSNotifications
。我确定通知启动的操作有效。我的设置可能有什么问题?
我使用单例feedViewController
从AppDelegate
's加载 a didFinishLaunching
。这feedViewController
保留了对 a 的引用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]];
}