NSNotificationCenter用法实例
来源:互联网 发布:cf手游刷枪软件图片 编辑:程序博客网 时间:2024/05/16 06:28
有时候需要两个界面传递值,以前用过protocol方法传过,不过不是很实用。
NSNotificationCenter很适合在两个界面传递。
下面把我写的方法粘贴上来
接受信息的类 accept.m-
static accept *m_instance;
+(accept *)getInstance{
return m_instance;
}
- (id)initWithStyle:(UITableViewStyle)style {
// Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
self = [super initWithStyle:style];
if (self) {
// Custom initialization.
m_instance=self;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self addEventListeners];
}
-(void) addEventListeners
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(dealReceiveEvent:)
name:notificationName
object:self];
}
-(void) removeEventListeners
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:notificationName object:self];
}
- (void)dealloc {
[super dealloc];
[self removeEventListeners];
}
发送信息的类sender.m
可以直接调用doBack方法。
我这里传了一个CGPoint参数。
- (void) doBack:(CGPoint)offset
{
if (m_curSelectedItem != -1) {
NSString *curselected=[[NSString alloc] initWithFormat:@"%d",m_curSelectedItem];
NSString *m_offset=[[NSString alloc] initWithFormat:@"%f",offset.x];
NSMutableDictionary *dic= [[NSMutableDictionary alloc] init];
[dic setObject:curselected forKey:@"1"];
[dic setObject:m_offset forKey:@"2"];
[[NSNotificationCenter defaultCenter] postNotificationName:notificationName
object:[accept getInstance]
NSNotificationCenter很适合在两个界面传递。
下面把我写的方法粘贴上来
接受信息的类 accept.m-
static accept *m_instance;
+(accept *)getInstance{
}
- (id)initWithStyle:(UITableViewStyle)style {
}
- (void)viewDidLoad {
}
-(void) addEventListeners
{
}
-(void) removeEventListeners
{
}
- (void)dealloc {
}
发送信息的类sender.m
可以直接调用doBack方法。
我这里传了一个CGPoint参数。
- (void) doBack:(CGPoint)offset
{