代码之家  ›  专栏  ›  技术社区  ›  JuValencia

当applicationWillEnterForeground发生时验证剪贴板(swift)

  •  0
  • JuValencia  · 技术社区  · 10 年前

    我正在构建一个应用程序,允许将图像从safari搜索复制到图像视图,当我重新打开该应用程序时,如果我处于特定视图(下图),我需要验证剪贴板是否为空,我想启用粘贴按钮。 enter image description here

    我想知道我怎样才能在swift中做到这一点。

    再次提前感谢。

    1 回复  |  直到 10 年前
        1
  •  0
  •   Felix    10 年前

    您可以像这样检查剪贴板上的图像:

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        if UIPasteboard.generalPasteboard().image != nil {
            pasteButton.enabled = true
        } 
        else {
            pasteButton.enabled = false
        }
    }
    

    如果要使用该事件,可以执行以下操作:

    override func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().addObserver(self, 
        selector: Selector("updatePasteButton"), 
        name: UIApplicationWillEnterForegroundNotification, 
        object: nil)
    }
    
    func updatePasteButton() {
        if UIPasteboard.generalPasteboard().image != nil {
            pasteButton.enabled = true
        } 
        else {
            pasteButton.enabled = false
        }
    }
    

    updatePasteButton()将在每次事件发生时被调用。