ローカル通知を送信しようとしています。通知を送信するクラスのコードは次のとおりです。
@interface Sender : UIView
{
NSInteger itemID;
}
@implementation Sender
-(void) changedProperty
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationName" object:NULL];
}
この通知を受け取るコードは次のとおりです。
@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
}
しかし、このコードは機能しません。デバッグpostNotificationName: オブジェクトがどのように機能するかはわかりますが、 selectedItem:メソッドは呼び出されません
アップデート。 これがより多くのコードです。多分これが役立つでしょう。
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