I try to send local notification. Here some code for class sending the notification:
@interface Sender : UIView
{
NSInteger itemID;
}
@implementation Sender
-(void) changedProperty
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationName" object:NULL];
}
And here the code to receive this notification:
@interface Listener : UIViewController
{
}
@implementation Listener
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectedItem:) name:@"NotificationName" object:NULL];
}
- (void)viewDidUnload
{
[super viewDidUnload];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"NotificationName" object:NULL];
}
-(void) selectedItem:(NSNotification *) notification
{
// some actions
}
But this code doesn’t work. Debugging I see how postNotificationName: object works but the method selectedItem: doesn’t call
UPDATE.
Here is more code. Maybe this will help.
extern const NSString* selectItemNotificationName;
@interface vRoomSelectorItem : UIView
{
RoomSelectorItemBackground backgroundType;
NSInteger itemID;
}
@property NSInteger itemID;
-(void) setBackgroundType:(RoomSelectorItemBackground) backgroundType;
@interface vRoomSelectorItem ()
@property RoomSelectorItemBackground backgroundType;
@end
@implementation vRoomSelectorItem
const NSString* selectItemNotificationName = @"Reservation.SelectRoom";
-(RoomSelectorItemBackground) backgroundType
{
return backgroundType;
}
-(void) setBackgroundType:(RoomSelectorItemBackground)value
{
if(backgroundType != value)
{
backgroundType = value;
[self changedBackgroundType];
}
}
-(void) changedBackgroundType
{
if(backgroundType == RoomSelectorItemFilled)
{
// animation
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:(NSString*)selectItemNotificationName object:NULL userInfo:[[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInteger:itemID], @"ID", NULL]];
});
}
else
// reverse animation
}
#import "vRoomSelectorItem.h"
@interface vcReservationSelectRoom : UIViewController
{
NSMutableArray* arraySelectorItems;
}
@implementation vcReservationSelectRoom
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectedItem:) name:(NSString*)selectItemNotificationName object:NULL];
for(NSInteger i = 1; i <= SELECTOR_ITEM_COUNT; ++i)
{
vRoomSelectorItem* newItem = [[vRoomSelectorItem alloc] initWithFrame:CGRectMake(/*coordinates*/)];
[self.view addSubview:newItem];
[newItem setBackgroundType:RoomSelectorItemTransparent];
[newItem setItemID:i];
[arraySelectorItems addObject:newItem];
newItem = NULL;
}
}
-(void) selectedItem:(NSNotification *) notification
{
// some actions
}
-(void) dealloc
{
arraySelectorItems = NULL;
[[NSNotificationCenter defaultCenter] removeObserver:self name:(NSString*)selectItemNotificationName object:NULL];
}
@end
Ok, I’ve solved the problem. The piece of code above is not enough to understand, why it has not been working. The object of class vcReservationSelectRoom was temporary and it had been destroyed before sending notification from any vRoomSelectorItem. Sorry for my mistake of not showing enough code to solve this problem. And thank you all for helping.