Ios 无法接收通知
我尝试发送本地通知。下面是一些用于类发送通知的代码:Ios 无法接收通知,ios,notifications,Ios,Notifications,我尝试发送本地通知。下面是一些用于类发送通知的代码: @interface Sender : UIView { NSInteger itemID; } @implementation Sender -(void) changedProperty { [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationName" object:NULL]; } @interface Listene
@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
}
下面是接收此通知的代码:
@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
extern const NSString*selectItemNotificationName;
@接口vRoomSelectorItem:UIView
{
RoomSelectorItemBackgroundType;
NSInteger-itemID;
}
@属性NSInteger itemID;
-(void)setBackgroundType:(RoomSelectorItemBackground)backgroundType;
@接口vRoomSelectorItem()
@属性RoomSelectorItemBackgroundType;
@结束
@vRoomSelectorItem的实现
const NSString*selectItemNotificationName=@“Reservation.SelectRoom”;
-(RoomSelectorItemBackground)背景类型
{
返回背景类型;
}
-(void)setBackgroundType:(RoomSelectorItemBackground)值
{
if(背景类型!=值)
{
背景类型=值;
[自换背景类型];
}
}
-(无效)changedBackgroundType
{
如果(backgroundType==RoomSelectorItemFilled)
{
//动画
dispatch\u async(dispatch\u get\u main\u queue()^{
[[NSNotificationCenter defaultCenter]postNotificationName:(NSString*)selectItemNotificationName对象:NULL用户信息:[[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInteger:itemID],@“ID”,NULL];
});
}
其他的
//反向动画
}
#导入“vRoomSelectorItem.h”
@界面vcReservationSelectRoom:UIViewController
{
NSMUTABLEARRY*阵列选择项;
}
@实现vcReservationSelectRoom
-(无效)viewDidLoad
{
[超级视图下载];
[[NSNotificationCenter defaultCenter]添加观察者:自选择器:@selector(selectedItem:)name:(NSString*)selectItemNotificationName对象:NULL];
对于(NSInteger i=1;i,您的代码似乎应该可以工作。请确保通知位于主线程上,并且工作流如下所示:
将侦听器添加到通知中心
发起发送方
发送通知
您可以确保它位于主线程上,具有:
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] post...];
});
有两件事我会改变:
- 将自己从
-(void)dealloc
中的NSNotificationCenter
中移除
属于-(void)viewDidUnload
。viewDidUnload将被弃用,并且
在没有viewDidUnload的情况下可以调用dealloc
- 对于通知,我喜欢将名称存储在外部常量中
要确保我没有键入字符串,请执行以下操作:
//header.h
extern NSString *const notificationName;
//implementation.m
NSString *const notificationName = @"notification";
根据问题中的代码,我认为您可以尝试以下方法:
- (void)viewDidLoad
{
好的,我已经解决了这个问题。上面的代码还不足以理解为什么它不能工作。类vReservationSelectRoom的对象是临时的,在从任何vRoomSelectorItem发送通知之前它已被销毁。很抱歉,我没有显示足够的代码来解决这个问题问题。谢谢大家的帮助。你什么时候发布通知?是在添加观察者之后还是之前?我已经检查过了。观察者是在发布通知之前添加的。工作流如你所述。但是我如何确保通知在主线程上?在这个应用中,我不知道手动地吃任何线程。因此它应该在主线程中。刚刚检查过。不幸的是它仍然不起作用。我不明白为什么会这样。尝试重新键入选择器和通知。十分之九的问题是键入选择器和通知是正确的。在主题中添加了更多代码。希望它有用。这是无用的对我来说,因为方法changedProperty是类发送者的内部方法