Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.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 13被驳回_Ios_Swift_Uiviewcontroller_Uikit_Ios13 - Fatal编程技术网

检测表在iOS 13被驳回

检测表在iOS 13被驳回,ios,swift,uiviewcontroller,uikit,ios13,Ios,Swift,Uiviewcontroller,Uikit,Ios13,在iOS 13之前,显示的视图控制器用于覆盖整个屏幕。并且,当解除时,执行父视图控制器viewdideappear功能 现在iOS 13将视图控制器显示为默认的工作表,这意味着卡将部分覆盖基础视图控制器,这意味着不会调用viewdideappear,因为父视图控制器从未实际消失 是否有方法检测显示的视图控制器工作表已被取消?我可以在父视图控制器中替代某些其他功能,而不是使用某种委托 是否有方法检测显示的视图控制器工作表已被取消 对 我可以在父视图控制器中替代某些其他函数,而不是使用某种委托 不,

在iOS 13之前,显示的视图控制器用于覆盖整个屏幕。并且,当解除时,执行父视图控制器
viewdideappear
功能

现在iOS 13将视图控制器显示为默认的工作表,这意味着卡将部分覆盖基础视图控制器,这意味着不会调用
viewdideappear
,因为父视图控制器从未实际消失

是否有方法检测显示的视图控制器工作表已被取消?我可以在父视图控制器中替代某些其他功能,而不是使用某种委托

是否有方法检测显示的视图控制器工作表已被取消

我可以在父视图控制器中替代某些其他函数,而不是使用某种委托


不,“某种委托”是你如何做的。让自己成为演示控制器的委托人,并覆盖
PresentationControllerDidDisclease(:)



缺少一般运行时生成的事件来通知您显示的视图控制器(无论是否全屏显示)已被取消,这确实很麻烦;但这并不是一个新问题,因为始终存在非全屏显示的视图控制器。只是现在(在iOS 13中)它们的数量更多了!我在其他地方专门就这个主题提出了一个单独的问题和答案:。

对于未来的读者,这里有一个更完整的答案,包括实现:

  • 在根视图控制器准备segue中添加以下内容(假设您的modal具有导航控制器)
  • 在模态视图控制器中,添加以下委托+方法
  • 在模态视图控制器中确保以下属性为true,以便调用委托方法
  • 利润

  • 下面是一个父视图控制器的代码示例,当它显示为图纸的子视图控制器(即默认的iOS 13方式)被取消时,会收到通知:

    公共最终类父类:UIViewController、UIAdaptivePresentationControllerDelegate
    {
    //这是假设segue是一个故事板segue;
    //如果您是手动演示,只需在那里看到学员即可。
    public override func prepare(对于segue:UIStoryboardSegue,发送方:Any?)
    {
    如果segue.identifier==“mySegue”{
    segue.destination.presentationController?.delegate=self;
    }
    }
    
    公共职能代表控制器( _presentationController:UIPresentationController) { //仅在通过拖动取消图纸时调用。 //如果你给孩子打电话,你需要一些额外的东西。 //(我在孩子身上发现了压倒一切的解雇
    //presentationController.delegate?.presentationController.delegate //工作正常)。 } }

    Jerland2的答案很混乱,因为(a)原始提问者希望在工作表被撤销时得到函数调用(而他实现了PresentationControllerDidAttemptoDismiss,当用户尝试撤销工作表失败时调用该函数),以及(b)设置isModalInPresentation是完全正交的,实际上会使显示的图纸不可拆卸(这与OP想要的相反)。

    另一个返回
    视图的选项将出现
    ,并且
    视图显示
    已设置

    let vc = UIViewController()
    vc.modalPresentationStyle = .fullScreen
    

    此选项覆盖全屏,并在关闭后调用上述方法

    调用presentingViewController.viewWillDisplay不是很简单吗? 解雇之前

    self.presentingViewController?.viewWillAppear(false)
    self.dismiss(animated: true, completion: nil)
    

    在我看来,苹果不应该将
    pageSheet
    设置为默认的
    modalPresentationStyle

    我想通过使用
    swizzling

    像这样:

    private func _swizzling(forClass: AnyClass, originalSelector: Selector, swizzledSelector: Selector) {
        if let originalMethod = class_getInstanceMethod(forClass, originalSelector),
           let swizzledMethod = class_getInstanceMethod(forClass, swizzledSelector) {
            method_exchangeImplementations(originalMethod, swizzledMethod)
        }
    }
    
    extension UIViewController {
    
        static func preventPageSheetPresentationStyle () {
            UIViewController.preventPageSheetPresentation
        }
    
        static let preventPageSheetPresentation: Void = {
            if #available(iOS 13, *) {
                _swizzling(forClass: UIViewController.self,
                           originalSelector: #selector(present(_: animated: completion:)),
                           swizzledSelector: #selector(_swizzledPresent(_: animated: completion:)))
            }
        }()
    
        @available(iOS 13.0, *)
        private func _swizzledPresent(_ viewControllerToPresent: UIViewController,
                                            animated flag: Bool,
                                            completion: (() -> Void)? = nil) {
            if viewControllerToPresent.modalPresentationStyle == .pageSheet
                       || viewControllerToPresent.modalPresentationStyle == .automatic {
                viewControllerToPresent.modalPresentationStyle = .fullScreen
            }
            _swizzledPresent(viewControllerToPresent, animated: flag, completion: completion)
        }
    }
    
    然后将此行放到您的
    AppDelegate

    UIViewController.preventPageSheetPresentationStyle()
    

    拖动或调用Disclease FUNC将使用以下代码

    1) 在根视图控制器中,您可以告诉它哪个是它的表示视图控制器,如下代码所示

     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "presenterID" {
            let navigationController = segue.destination as! UINavigationController
            if #available(iOS 13.0, *) {
                let controller = navigationController.topViewController as! presentationviewcontroller
                // Modal Dismiss iOS 13
                controller.presentationController?.delegate = self
            } else {
                // Fallback on earlier versions
            }
            navigationController.presentationController?.delegate = self
    
        }
    }
    
    2) 同样,在根视图控制器中,您可以告诉用户当其表示视图控制器失效时将执行什么操作

    public func presentationControllerDidDismiss(
      _ presentationController: UIPresentationController)
    {
        print("presentationControllerDidDismiss")
    }
    
    1) 在演示视图控制器中,当您点击此图片中的“取消”或“保存”按钮时。将调用下面的代码

    self.dismiss(animated: true) {
            self.presentationController?.delegate?.presentationControllerDidDismiss?(self.presentationController!)
        }
    

    覆盖
    视图将在被解除的
    UIViewController
    上消失。它将通过
    isbeingdisembleed
    boolean标志提醒您解雇

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    
        if isBeingDismissed {
            print("user is dismissing the vc")
        }
    }
    
    **如果用户在向下刷卡的中途将卡刷回,即使卡没有被解除,它仍然会注册为被解除。但这是一个你可能不关心的边缘案例。

    Swift

    调用的通用解决方案
    视图将出现在iOS13

    class ViewController: UIViewController {
    
            override func viewWillAppear(_ animated: Bool) {
                super.viewWillAppear(animated)
                print("viewWillAppear")
            }
    
            //Show new viewController
            @IBAction func show(_ sender: Any) {
                let newViewController = NewViewController()
                //set delegate of UIAdaptivePresentationControllerDelegate to self
                newViewController.presentationController?.delegate = self
                present(newViewController, animated: true, completion: nil)
            }
        }
    
        extension UIViewController: UIAdaptivePresentationControllerDelegate {
            public func presentationControllerDidDismiss( _ presentationController: UIPresentationController) {
                if #available(iOS 13, *) {
                    //Call viewWillAppear only in iOS 13
                    viewWillAppear(true)
                }
            }
        }
    

    如果在全屏中使用ModalPresentationStyle,控制器的行为将恢复正常

    consultarcontrollerconsultar=this.Storyboard.instanceeviewcontroller(“ConsultarController”)作为ConsultarController; controllerConsultar.ModalPresentationStyle=UIModalPresentationStyle.FullScreen;
    this.NavigationController.PushViewController(controllerConsultar,true)

    如果有人无法访问显示的视图控制器,他们可以在显示视图控制器时覆盖以下方法,并将
    modalPresentationStyle
    更改为
    fullScreen
    ,或者使用此方法添加上述策略之一

     override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
        if let _ = viewControllerToPresent as? TargetVC {
            viewControllerToPresent.modalPresentationStyle = .fullScreen
        }
        super.present(viewControllerToPresent, animated: flag, completion: completion)
    }
    
    如果显示的视图控制器是导航控制器,并且您希望检查根控制器,则可以将上述条件更改为

    if let _ = (viewControllerToPresent as? UINavigationController)?.viewControllers.first as? TargetVC {
       viewControllerToPresent.modalPresentationStyle = .fullScreen
    }
    

    如果您想在用户从该工作表中关闭模式工作表时执行某些操作。 让我们假设您已经有了一个带有
    @IBAction
    的关闭按钮和一个在closi之前显示警报的逻辑
    class ViewController: UIViewController {
    
            override func viewWillAppear(_ animated: Bool) {
                super.viewWillAppear(animated)
                print("viewWillAppear")
            }
    
            //Show new viewController
            @IBAction func show(_ sender: Any) {
                let newViewController = NewViewController()
                //set delegate of UIAdaptivePresentationControllerDelegate to self
                newViewController.presentationController?.delegate = self
                present(newViewController, animated: true, completion: nil)
            }
        }
    
        extension UIViewController: UIAdaptivePresentationControllerDelegate {
            public func presentationControllerDidDismiss( _ presentationController: UIPresentationController) {
                if #available(iOS 13, *) {
                    //Call viewWillAppear only in iOS 13
                    viewWillAppear(true)
                }
            }
        }
    
     override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
        if let _ = viewControllerToPresent as? TargetVC {
            viewControllerToPresent.modalPresentationStyle = .fullScreen
        }
        super.present(viewControllerToPresent, animated: flag, completion: completion)
    }
    
    if let _ = (viewControllerToPresent as? UINavigationController)?.viewControllers.first as? TargetVC {
       viewControllerToPresent.modalPresentationStyle = .fullScreen
    }
    
    class MyModalSheetViewController: UIViewController {
    
         override func viewDidLoad() {
            super.viewDidLoad()
    
            self.presentationController?.delegate = self
         }
    
         @IBAction func closeAction(_ sender: Any) {
             // your logic to decide to close or not, when to close, etc.
         }
    
    }
    
    extension MyModalSheetViewController: UIAdaptivePresentationControllerDelegate {
    
        func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool {
            return false // <-prevents the modal sheet from being closed
        }
    
        func presentationControllerDidAttemptToDismiss(_ presentationController: UIPresentationController) {
            closeAction(self) // <- called after the modal sheet was prevented from being closed and leads to your own logic
        }
    }
    
    func sheet<Item, Content>(item: Binding<Item?>, onDismiss: (() -> Void)?, content: (Item) -> Content) -> some View