簡體   English   中英

如何檢測彈出窗口何時在 iOS 9 中被關閉

[英]How to detect when a popover is dismissed in iOS 9

我正在更新應用程序以使用通用故事板。 我已經使用界面構建器通過從按鈕拖動到我的新視圖控制器並選擇“呈現為彈出框”作為一種轉場,為新的視圖控制器創建了一個彈出框轉場。

當用戶在彈出窗口之外按下(關閉它)時,我需要在呈現視圖控制器中收到通知,以便我可以撤消他們的操作。 我怎樣才能做到這一點?

通常我會手動創建 popover 並使我的 viewcontroller 成為 popover 的委托; 允許我使用 popoverControllerDidDismissPopover 委托回調。 但是,這在 iOS9 中已被棄用,即使不是,我也不知道在哪里可以找到彈出框,因此我可以將其委托設置為我的視圖控制器。

不確定您指的是哪種方法已被棄用,但您仍然可以使用UIPopoverPresentationControllerDelegate來實現這一點。 就像是:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "popoverSegue" {
        let vc = segue.destinationViewController
        sortVC.modalPresentationStyle = .Popover
        sortVC.popoverPresentationController?.sourceRect = filterButton.bounds
        sortVC.preferredContentSize = CGSizeMake(216, 150)
        sortVC.popoverPresentationController!.delegate = self
    }
}

然后使用

func popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController)

處理其解雇的方法。

popoverControllerDidDismissPopover:方法已替換為popoverPresentationControllerShouldDismissPopover:因為UIPopoverControllerDelegate已替換為UIPopoverPresentationControllerDelegate

從你的呈現視圖控制器,遵守新協議並在prepareForSegue:設置 popover 呈現控制器的委托:

class MyPresentingViewController: UIViewController, UIPopoverPresentationControllerDelegate {

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {         
        if let popoverPresentationController = segue.destinationViewController.popoverPresentationController {
            popoverPresentationController.delegate = self
        }
    }

    func popoverPresentationControllerShouldDismissPopover(popoverPresentationController: UIPopoverPresentationController) -> Bool {
        return true
    }
}

然后,您可以使用委托方法以您之前想要的方式處理解雇檢測。

此問題的更新答案。

所有功勞都歸功於這個答案

您必須在 iOS 13 上使用的方法: - (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController

UIPopoverPresentationControllerDelegate繼承自UIAdaptivePresentationControllerDelegate ,其中包含了 Beto 指出的presentationControllerShouldDismisspresentationControllerDidDismiss

我只是將這些函數的UIAdaptivePresentationControllerDelegate版本中的代碼移動到UIAdaptivePresentationControllerDelegate版本,它們的工作方式與以前完全相同。

不必更改視圖控制器上的委托聲明或設置isModalInPresentation

原始代碼在 13.2.3 下仍然有效,但這些功能已貶值,總有一天它們會停止工作……還是不行?

我的應用程序是一個使用彈出框而不是演示文稿或卡片樣式的 iPad 應用程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM