无法收到通知

时间:2012-08-06 10:57:31

标签: ios notifications

我尝试发送本地通知。这里有一些代码用于发送通知的类:

@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:object 如何工作但方法 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

3 个答案:

答案 0 :(得分:0)

看起来您的代码应该可行。确保通知在主线程上,工作流程如下:

  1. 将监听器添加到通知中心
  2. 发起发件人
  3. 发送通知
  4. 您可以确定它位于主线程上:

    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] post...];
    });
    

    我会改变一些事情:

    • 取而代之的是NSNotificationCenter中的-(void)dealloc -(void)viewDidUnload。 viewDidUnload将被弃用,并且 没有viewDidUnload就可以调用dealloc。

    • 对于通知,我喜欢将名称存储在外部常量中 确保我不拼错字符串:

      //header.h
      extern NSString *const notificationName;
      
      
      //implementation.m
      NSString *const notificationName = @"notification";
      

答案 1 :(得分:0)

从问题中的代码我想你可以试试这个:

- (void)viewDidLoad

{

[super viewDidLoad];        

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectedItem:) name:@"NotificationName" object:NULL];

[self.view changedProperty]; // method of your Sender class

}

答案 2 :(得分:0)

好的,我已经解决了这个问题。上面的代码不足以理解,为什么它没有工作。类 vcReservationSelectRoom 的对象是临时的,在从任何 vRoomSelectorItem 发送通知之前它已被销毁。很抱歉我没有显示足够的代码来解决这个问题。谢谢大家的帮助。