iOS 13使用按钮关闭模式页面

时间:2019-10-23 08:01:26

标签: ios swift ios13 modalviewcontroller dismiss

因此,在关闭iOS 13中的页面工作表模式屏幕时,我需要一些帮助。我看了几篇文章,没有任何帮助。

iOS 13 Modals - Calling swipe dismissal programmatically

看着第二个答案,我尝试过,但是我一直在隐含自我,需要明确。试图研究,但没有发现太多。

我要做的就是在导航栏的右上方有一个“完成”按钮。当按下该按钮时,我需要它连接到数据库并保存有效的数据(已实现)。然后,它需要关闭模式屏幕并刷新表,以获取最新的更改。

我已经阅读了代表的内容,但并没有太大帮助。我对UIAdaptivePresentationControllerDelegate有所了解,并且了解其背后的基本知识,并且能够拉动以关闭模态。但不确定如何在按钮中重复该操作。我确实记得阅读过设置演讲代表的内容。或类似的东西。

let navigationController = segue.destination as! UINavigationController
let editSensorVC = navigationController.topViewController as! EditSensorViewController

navigationController.presentationController?.delegate = editSensorVC

是说这是我设置代表的方式,但不确定从何处去。

2 个答案:

答案 0 :(得分:1)

如果原始VC的类型为PresentingVC,而模式的类型为PresentedVC,我将使用以下方法。给定上面的segue语句,我假设您正在使用情节提要,但如果未实例化,则在实例化PresentedVC

时,应通过注入委托值来替换prepare(for segue :)

对于初学者,通过定义委托协议并提供委托属性,将PresentedVC设置为容纳委托。


protocol PresentedVCDelegate {
   func presentedVCDidUpdateDatabase()
}

class PresentedVC {
    var delegate: PresentedVCDelegate?

    @IBAction buttontapped(_ sender: Any) {
        //existing code to validate and save data to databse
        delegate?. presentedVCDidUpdateDatabase()
        dismiss(animated: true)
    }
}

更新PresentingVC,以便在实例化其子VC时将自身作为委托注入:

class PresentingVC {

    //all the current code

    // and amend preapre(for:) something like
    func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      // Get the new view controller.
       if let presented = segue.destination as? PresentedVC {
          presented.delegate = self
          //anything else you do already
       }
    }
}

扩展它以支持协议方法

extension PresentingVC: PresentedVCDelegate {
    func presentedVCDidUpdateDatabase() {
        tableView.reloadData()
        //any other work necessary after PresentedVC exits
    }
{

注意:是从内存中写入的而不是经过编译的,因此可能包含较小的错别字,但希望它有足够的细节来阐明概念吗?

答案 1 :(得分:0)

在这里,这是您如何在呈现viewcontroller的objective-c中做到这一点,它实际上并没有呈现任何内容,但实际上是Navigationcontroller。这是简单模式。

UIViewController *pvc = [UIViewController new];
WLGNavigationController *nav = [[WLGNavigationController alloc] initWithRootViewController:pvc];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(backerPressed)];
[[pvc navigationItem] setLeftBarButtonItem:backBarButtonItem];
[[self navigationController] presentViewController:nav animated:true completion:nil];

按下支持者是这样的:

- (void)backerPressed {
  [[self navigationController] dismissViewControllerAnimated:true completion:nil];
}

对于硬模式:

完成处理程序:

在要呈现的VC上添加完成处理程序,然后在所呈现的VC中添加导航按钮,然后添加一个操作,当按下该操作时,在完成诺言执行后运行该完成处理程序。然后在呈现视图控制器中,设置完成处理代码,然后关闭呈现的导航/视图控制器组合。

委托:

在您要呈现的VC中编写一个委托,当promise返回时,执行委托方法,“对选择器响应”等等,就像您通常对委托所做的那样。在呈现的VC中,然后使其成为要呈现的VC的委托,实现一种方法,该方法在被调用时调用导航控制器以消除其所呈现的Navigation / viewcontroller程序包。