Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.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/8/swift/16.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 在父UIViewController中接收到多个NSN通知_Ios_Swift_Nsnotificationcenter_Nsnotifications - Fatal编程技术网

Ios 在父UIViewController中接收到多个NSN通知

Ios 在父UIViewController中接收到多个NSN通知,ios,swift,nsnotificationcenter,nsnotifications,Ios,Swift,Nsnotificationcenter,Nsnotifications,我有一个用于某些视图的父级UIViewControllerBaseViewController: class TestViewController: BaseViewController { ... 我想在BaseViewController中接收通知,因此我在其视图中添加了一个观察者,它将出现: NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(BaseViewController.recei

我有一个用于某些视图的父级
UIViewController
BaseViewController

class TestViewController: BaseViewController { ...
我想在
BaseViewController
中接收通知,因此我在其
视图中添加了一个观察者,它将出现

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(BaseViewController.receivedNotification(_:)), name:"NotificationIdentifier", object: nil)
我无法删除
视图上的观察者将消失
,因为通知是从呈现的
UIViewController
发送的,该通知可以在每个
UIViewController
上呈现,并且父级
BaseViewController
,因此
BaseViewController
始终消失

因此,在应用程序中导航后,会有多个观察者添加,我会多次收到通知

我应该怎么做才能只执行一次通知选择器?或者,当按下另一个
UIViewController
时,如何
.removeObserver
(但在显示时不删除)

我无法删除ViewWillEnglish上的观察者,因为通知是从显示的UIViewController发送的,可以在每个UIViewController上显示父对象是BaseViewController,因此BaseViewController始终消失

你绝对可以。屏幕上显示的每个新的
ViewController
都有自己的实例,因此它有自己的
BaseViewController
副本。事实上,当前只能显示一个
BaseViewController
副本(如果我们谈论的是全屏视图控制器)

我无法删除ViewWillEnglish上的观察者,因为通知是从显示的UIViewController发送的,可以在每个UIViewController上显示父对象是BaseViewController,因此BaseViewController始终消失

你绝对可以。屏幕上显示的每个新的
ViewController
都有自己的实例,因此它有自己的
BaseViewController
副本。事实上,当前只能显示一个
BaseViewController
副本(如果我们谈论的是全屏视图控制器)

您可以很好地使用:

NSNotificationCenter.defaultCenter().removeObserver(observer: <The class from which you want to remove>, name: "Name of notification which you want to remove", object: nil)
NSNotificationCenter.defaultCenter().removeObserver(观察者:,名称:“要删除的通知的名称”,对象:nil)
您可以很好地使用:

NSNotificationCenter.defaultCenter().removeObserver(observer: <The class from which you want to remove>, name: "Name of notification which you want to remove", object: nil)
NSNotificationCenter.defaultCenter().removeObserver(观察者:,名称:“要删除的通知的名称”,对象:nil)

是。但当我呈现UIViewController时,所有观察者都将被移除,并且不会收到通知。呈现的UIViewController是密码,它的父项不是BaseViewController。@Roo并且您没有从
BaseViewController
子类化
UIViewController
?否。呈现的
UIViewController
父项不同。@Roo如果是,则将其子类化。或者在按下之前删除observer,或者在
prepareForSegue(u:sender:)
@Roo中,我发现您的架构很奇怪。通常,您不需要在控制器中捕获通知。任何视图控制器都应该能够通过get
viewwillbeen
call刷新数据/UI。只有控制器没有在运行时检查更改的简单方法,并且需要通知。是的。但当我呈现UIViewController时,所有观察者都将被移除,并且不会收到通知。呈现的UIViewController是密码,它的父项不是BaseViewController。@Roo并且您没有从
BaseViewController
子类化
UIViewController
?否。呈现的
UIViewController
父项不同。@Roo如果是,则将其子类化。或者在按下之前删除observer,或者在
prepareForSegue(u:sender:)
@Roo中,我发现您的架构很奇怪。通常,您不需要在控制器中捕获通知。任何视图控制器都应该能够通过get
viewwillbeen
call刷新数据/UI。只有控制器没有在运行时检查更改的简单方法,并且需要通知。