0

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

@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
4

3 回答 3

0

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

  1. 向通知中心添加监听器
  2. 发起发件人
  3. 发送通知

您可以确定它在主线程上:

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

我会改变几件事:

  • 将自己从NSNotificationCenterin-(void)dealloc而不是-(void)viewDidUnload. viewDidUnload 将被弃用,并且可以在没有 viewDidUnload 的情况下调用 dealloc。

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

    //header.h
    extern NSString *const notificationName;
    
    
    //implementation.m
    NSString *const notificationName = @"notification";
    
于 2012-08-06T11:16:19.707 回答
0

从您在问题中的代码中,我认为您可以尝试以下操作:

- (void)viewDidLoad

{

[super viewDidLoad];        

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

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

}
于 2012-08-06T11:27:37.930 回答
0

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

于 2012-08-07T11:26:36.987 回答