簡體   English   中英

如何使特定觀察者聽到NSNotification?

[英]how to make the NSNotification heard by the specific observer?

我想開發一個雲存儲應用程序,並且想要實現該用戶可以將文件或文件夾移動到另一個文件夾。

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

當我移動文件並選擇目標文件夾並單擊“確認移動”按鈕時,文件未移動到我選擇的文件夾中。

您實際上需要發布通知才能激活“觀察者”。

編輯:

請注意,如果要在loadMoveRequest方法中接收folderID,則應為addObserver中的“選擇器”,如下所示:

[[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];

}

您需要在編寫了移動文件功能的類/視圖控制器中按以下方式處理觀察者:

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

然后,您必須編寫名稱為MoveCopypopViewClicked函數,該函數將具有在該類/視圖控制器中移動文件的代碼。

然后您需要按以下方式觸發通知;

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

並通過此通知傳遞值,您可以將NSDictionary傳遞給userInfo

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM