Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用NSNotificationCenter在类之间传递对象-传递对象失败_Ios_Objective C_Nsnotificationcenter - Fatal编程技术网

Ios 如何使用NSNotificationCenter在类之间传递对象-传递对象失败

Ios 如何使用NSNotificationCenter在类之间传递对象-传递对象失败,ios,objective-c,nsnotificationcenter,Ios,Objective C,Nsnotificationcenter,我的代码: NSDictionary *dict = @{@"1": @"_infoView3", @"2": [NSNumber numberWithFloat:_showSelectionView.frame.size.height] }; [[NSNotificationCenter defaultCenter] addObserver:self

我的代码:

NSDictionary *dict = @{@"1": @"_infoView3",
                       @"2": [NSNumber numberWithFloat:_showSelectionView.frame.size.height]
                       };

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:@"UIKeyboardWillShowNotification"
                                           object:dict];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardDidHide:)
                                             name:@"UIKeyboardDidHideNotification"
                                           object:dict];
以及:

但当键盘显示或隐藏时,这两种方法都不起作用。 如果我传递object nil,而不是dict,那么这两种方法是有效的


我不知道问题出在哪里,请帮助我,谢谢。

这是因为
对象
参数设计用于指定要观察的特定对象,而不是将任意数据传递给调用的选择器

从:

notificationSender

观察者需要其通知的对象 接受;也就是说,只有此发件人发送的通知是 提交给观察员

如果通过
nil
,通知中心将不使用通知的 发送者决定是否将其交付给观察者


正如我所看到的,你们正试图在观察者一侧张贴对象。相反,请参见下面的示例

接收级

- (void)viewDidLoad {
    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(receiveNotification:) 
        name:@"myNotification"
        object:nil];
}

- (void)receiveNotification:(NSNotification *)notification
{
    if ([[notification name] isEqualToString:@"myNotification"]) {
        NSDictionary *myDictionary = (NSDictionary *)notification.object;
        //doSomething here.
    }
}
发送方类

- (void)sendNotification {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object:YOUR_DICTIONARY];
}

谢谢您的回答,我如何传递这些参数?@Uquihurboy您应该将它们存储在一个实例变量中。@Uquihurboy实际上不清楚它们应该如何传递,因为这取决于它们是如何使用的(例如,它们是否会从一刻变到下一刻)。因此,最好在被调用的选择器中确定它们的值,或者将它们存储在实例变量中。但是,如果没有更多的信息,我无法准确地告诉你。@Uquihurboy我认为我的答案解释了发生的事情,而另一个答案解决了另一个你没有问的问题。对不起,也许我不擅长解释,但我希望我能传递参数。
- (void)sendNotification {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object:YOUR_DICTIONARY];
}