這是我第一次嘗試NSNotification
,嘗試了幾個教程,但不知何故,它不工作。發送一個NSNotification之間的意見
基本上我發送一個字典到B類彈出子視圖(UIViewController
)和測試是否已收到。
任何人都可以告訴我我做錯了什麼?
A類
- (IBAction)selectRoutine:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:@"Right"
forKey:@"Orientation"];
[[NSNotificationCenter defaultCenter]
postNotificationName:@"PassData"
object:nil
userInfo:dictionary];
createExercisePopupViewController* popupController = [storyboard instantiateViewControllerWithIdentifier:@"createExercisePopupView"];
//Tell the operating system the CreateRoutine view controller
//is becoming a child:
[self addChildViewController:popupController];
//add the target frame to self's view:
[self.view addSubview:popupController.view];
//Tell the operating system the view controller has moved:
[popupController didMoveToParentViewController:self];
}
B類
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(receiveData:)
name:@"PassData"
object:nil];
}
- (void)receiveData:(NSNotification *)notification {
NSLog(@"Data received: %@", [[notification userInfo] valueForKey:@"Orientation"]);
}
在A級發送通知之前是否裝有B類? – bandejapaisa 2013-04-30 19:37:23
不,它尚未加載。那就是我需要將通知移到加載後的位置。正確回答,以便將其標記爲已回答。謝謝 – 2013-04-30 19:38:52
移動'[[NSNotificationCenter defaultCenter] postNotificationName:@「PassData」 object:nil userInfo:dictionary];'創建B類後 – andreagiavatto 2013-04-30 19:39:31