子UITableViewController无法从Parent UITableViewController接收通知?

时间:2014-02-18 04:03:04

标签: ios objective-c uitableview nsnotificationcenter nsnotification

正如问题所述,我正在向儿童TVC发送通知,并且该儿童TVC无法接收通知并执行选择器handleNotification: (NSNotification *) paramNotif。即我从该选择器中获取NSLog output

我的代码如下,非常自我解释。

我应该补充一点,我的父TVC也会收到来自模态场景的通知,并将自己从dealloc的中心移除,但是它应该仍然有效,因为它是父母?

Parent UITableViewController

viewDidAppear

 [self postNotificationToD5TVC];

postNotificationToD5TVC

NSString *stringWithBOOL = [NSString stringWithFormat:@"%i",[self.workoutRoutine boolValue]];

NSString *kNotifName2 = @"workout";

NSNotification *notification = [NSNotification notificationWithName:kNotifName2 object:self userInfo:@{@"kworkout": stringWithBOOL}];

[[NSNotificationCenter defaultCenter] postNotification:notification];

这样可以正常工作,因为我在method

之前已经stringWithBoolSegue的NSLog了

Child UITableViewController

我有一个变量BOOL workout

viewDidLoad

 [self setUpSliders:[self isRoutine]];

- (BOOL)isRoutine

NSString *kNotifName2 = @"workout";

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

[center addObserver:self
           selector:@selector(handleNotification:)
               name:kNotifName2
             object:nil;

BOOL isCorrectWorkout = workout;

return isCorrectWorkout;

handleNotification :( NSNotification *)paramNotif

NSString *workoutString = paramNotif.userInfo[@"kworkout"];
NSInteger workoutInt = [workoutString integerValue];
workout = workoutInt;

NSLog(@"Received notification in D5TVC and value of bool is %i",workout);

1 个答案:

答案 0 :(得分:0)

问题是我没有调用孩子TVC。因此,当从父项向孩子发送通知时,孩子不会收听通知。因此,当孩子TVC加载时,它不能“回去”并听取之前的通知。