Swift + iOS:如何将notification.object设置为nil?

时间:2016-06-29 03:47:53

标签: ios swift

每当网页视图中的视频尝试全屏时,我都会尝试将窗口对象设置为nil,因为我不想让它接管屏幕。我在我的控制器中有这个:

    NSNotificationCenter.defaultCenter().addObserverForName(
        UIWindowDidResignKeyNotification,
        object: self.view.window,
        queue: nil
    ) { notification in
        notification.object = nil // <------ doesn't work because it's read only
        print("Video is now fullscreen")
    }

但是,object属性是只读的。它可以设置为零吗?或者是否会有其他方式截取即将全屏的视频并从中提取视频网址?

提前致谢。

== UPDATE ==

正如Altimir所说,我已经尝试过这个:( Swift 2)

NSNotificationCenter.defaultCenter().addObserverForName(
  UIWindowDidResignKeyNotification,
  object: self.view.window,
  queue: nil
) { notification in
  do {
    try notification.object?.remove()
  } catch {
    print("failed")
  }
  print("Video is now fullscreen")
}

但是,这会破坏我的构建。enter image description here

==更新2 ==

我根据Ozgur的建议尝试了这个:

NSNotificationCenter.defaultCenter().addObserverForName(
  UIWindowDidResignKeyNotification,
  object: self.view.window,
  queue: nil
) { notification in
  do {
    try NSNotificationCenter.defaultCenter().removeObserver(self)
    print("success")
  } catch {
    print("failed")
  }
  print("Video is now fullscreen")
}

但是当我在网页浏览中点击YouTube视频时,视频仍然全屏显示。

1 个答案:

答案 0 :(得分:0)

试试这个(它在Swift 3上):

git push