如何让特定观察者听到NSNotification?

时间:2016-08-02 03:48:40

标签: ios objective-c

我想开发一个云存储应用,我想实现该用户可以将文件或文件夹移动到另一个文件夹。

-(void)setUpNote{

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

我设置了一个标记thisIsOnTop来标记位于顶部的视图。

-(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];
    }
}
}

当我移动文件并选择目标文件夹并单击确认 - 移动按钮时,文件未移动到我选择的文件夹。

2 个答案:

答案 0 :(得分:2)

您需要实际发布通知,以便“观察者”激活。

编辑:

注意,如果你想在loadMoveRequest方法中接收folderID,那么它应该是addObserver中的'selector',如下所示:

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

您发布通知并传递folderID:

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

如果您需要传递特定于通知的某些数据,可以将NSDictionary设置为userInfo,而不是传递nil

最后实现loadMoveRequest:方法,如下所示:

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

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

}

答案 1 :(得分:0)

你需要在你编写了移动文件的函数的类/ viewcontroller中按如下方式处理观察者;

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

然后你必须编写名为MoveCopypopViewClicked的函数,该函数将包含移动该类/ viewcontroler中文件的代码。

然后您需要按如下方式触发通知;

[[NSNotificationCenter defaultCenter] postNotificationName:@"MoveCopypopViewClickedNotification" object:nil userInfo:nil];

要通过此通知传递值,您可以将 NSDictionary 传递给 userInfo