LoginSignup
7
4

More than 3 years have passed since last update.

NotificationCenter.default.removeObserverの大切さを痛感した話

Last updated at Posted at 2020-06-28

起きたこと

IMG_0347 2.PNG IMG_0343.PNG
TableViewControllerとNavigationControllerでできているチャットアプリを作成していたところ、トーク画面でスクショすると自動送信されるメッセージが他のトーク画面でスクショしても送信されるバグが発見された。
IMG_0331 2.PNG

原因と処理

スクショの監視するObserVerをViewDidLoad()の中に書いていたのですが、

NotificationCenter.default.addObserver(self, selector: #selector(didTakeScreenshot), name: UIApplication.userDidTakeScreenshotNotification, object: nil)

NavigationController画面遷移をしたときに通知を外していないことが原因でした。

NotificationCenter.default.removeObserver(self,name: UIApplication.userDidTakeScreenshotNotification, object: nil)

viewWillDisappearの中で書くことで期待した処理をすることが出来ました。

7
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
4