现在的位置: 首页 > 综合 > 正文

ios 监听(NSNotification)的使用(在项目中已使用) .

2017年12月08日 ⁄ 综合 ⁄ 共 987字 ⁄ 字号 评论关闭

我这是ipad开发,在首次进入系统后,点击左边视图的添加按钮,然后presentModalViewController添加登陆用户的视图。

在视图保存数据是然后请求监听触发事件,在leftViewController页面进行请求的接受

1.注册监听器

-(void)goChange:(NSNotification *)notification

{

    //拿到通知内容。

    NSDictionary *dic = [notification
userInfo
];

    ZYRootViewController *rootView=[[ZYRootViewController
alloc] init];

    rootView.userListArray=[dic
objectForKey
:@"myTable"];

    [rootView.myTableView
reloadData
];

}

2.在loadview方法注册监听者

//注册监听者。

    [[NSNotificationCenter
defaultCenter]addObserver:self
selector:@selector(goChange:)
name:@"myTableView"
object:nil]; 
//收到通知后,执行方法:goChange:,object:nil表示所有的用户

3.点击保存按钮的请求触发事件(执行监听者选择器的方法)

    User *user=[[User
alloc] init];

    ZYRootViewController *rootView=[[ZYRootViewController
alloc] init];

    rootView.userListArray=[user
getUser
];

    [rootView.myTableView
reloadData
];

    [user release];

    [rootView release];

     NSDictionary *dic = [NSDictionary
dictionaryWithObject:rootView.userListArray
forKey:@"myTable"];

    [[NSNotificationCenter
defaultCenter]postNotificationName:@"myTableView"
object:self
userInfo:dic];

抱歉!评论已关闭.