Matrix Matrix - 4 months ago 15
Objective-C Question

how to make the NSNotification heard by the specific observer?

I want to develop a cloud storage app and I want to achieve that user can move a file or folder to another folder.

-(void)setUpNote{

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MoveCopypopViewClicked:) name:@"MoveCopypopViewClickedNotification" object:nil];
}


I set a mark thisIsOnTop to mark the view which is on the top.

-(void)viewWillAppear:(BOOL)animated{
self.thisIsOnTop = YES;

[super viewWillAppear:YES];

self.myPopView = [[MoveCopyPopView alloc]initWithFrame:CGRectMake(0, screenH, screenW, bottomH)];

[HBKeyWindow addSubview:self.myPopView];

[UIView animateWithDuration:0.5 animations:^{
self.myPopView.frame = CGRectMake(0, screenH - bottomH, screenW, bottomH);
}];
[self setupRefresh];
}

-(void)viewWillDisappear:(BOOL)animated{
self.thisIsOnTop = NO;
if (self.tableViewStatus == 1) {
//[self.tableView setEditing:!self.tableView.editing animated:YES];
self.tableViewStatus = 0;
[self disapperPopView];
}
}

-(void)MoveCopypopViewClicked:(NSNotification *)text{
NSDictionary * dict = text.userInfo;
NSString * btnClicked = [dict objectForKey:@"btnClicked"];
self.mycurrentPOPBtnClicked = btnClicked;

if ([self.myChooseType isEqualToString:@"file"]) {
if ([btnClicked isEqualToString:@"CreateFolderBtnClicked"]) {

[self showNewFolderView];

}else if ([btnClicked isEqualToString:@"ConfirmBtnClicked"]){
[[NSNotificationCenter defaultCenter] postNotificationName:@"MoveCopypopViewClickedNotification" object:nil userInfo:self.prepareToPushFolderID];

for (UIViewController *temp in self.navigationController.viewControllers) {
if ([temp isKindOfClass:[MyDesignTableViewController class]]) {
[self.navigationController popToViewController:temp animated:YES];
}
}

[self disapperPopView];
[self.tableView reloadData];
[self.tableView.mj_header beginRefreshing];
}
}else if ([self.myChooseType isEqualToString:@"folder"]){
if ([btnClicked isEqualToString:@"CreateFolderBtnClicked"]) {
[self showNewFolderView];

}else if ([btnClicked isEqualToString:@"ConfirmBtnClicked"]){

[self loadMoveFolderRequest];

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:2] animated:YES];

[self disapperPopView];
[self.tableView reloadData];
[self.tableView.mj_header beginRefreshing];
}
}
}


when I moved a file and choose the destination folder and click the confirm-move button ,the file wasn't moved to the folder I choose.

Answer

You need to actually post the notification in order for the 'observer' to activate.

EDIT:

Note that if you want to receive the folderID in loadMoveRequest method, then it should be the 'selector' in addObserver like so:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadMoveRequest:) name:@"MoveCopypopViewClickedNotification" object:nil];

The you post the notification and pass the folderID:

[[NSNotificationCenter defaultCenter] postNotificationName:@"MoveCopypopViewClickedNotification" object:nil userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:folderID] forKey:@"folderID"]];

In case you need to pass some data specific to the notification, you could do so by setting a NSDictionary to userInfo instead of passing nil.

Finally implement the loadMoveRequest: method like so:

- (void)loadMoveRequest:(NSNotification *notification) {

    NSInteger folderId = [[[notification userInfo] objectForKey:@"folderID"] integerValue];

}