我想在呈现模态的视图控制器中检测到模态解雇。
此方法对于在新的卡模式上检测到新的iOS 13滑动消除效果非常出色:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "MyIdentifier" {
segue.destination.presentationController?.delegate = self
}
}
extension MyController: UIAdaptivePresentationControllerDelegate {
func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
//clean up UI (de-selecting stuff) once modal has been dismissed
}
}
但是,如果模态通过动作以编程方式自行退出,则不会调用presentationControllerDidDismiss:
@IBAction func btnDismissTap(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
这是一个错误,还是有一种方法可以通过编程方式调用任何“滑动”关闭,因此我可以以相同的方式检测所有关闭?目前,我正在为我的模态编写额外的“解雇”委托方法,以解决此问题,这似乎是不必要的。
答案 0 :(得分:6)
但是,如果模态通过动作以编程方式自行退出,则不会调用presentationControllerDidDismiss
library(tidyverse) # create dataframe with 2 columns df <- tibble( "10+ years" = c(0, 2, 4, 5), "better_named_column" = c(2, 3, 4, 5) ) # column names before names(df) #> [1] "10+ years" "better_named_column" # check that replacing "+ years" with empty string looks as expected gsub("\\+ years", "", names(df)) #> [1] "10" "better_named_column" # apply to actual dataframe names(df) <- gsub("\\+ years", "", names(df)) head(df) #> # A tibble: 4 x 2 #> `10` better_named_column #> <dbl> <dbl> #> 1 0 2 #> 2 2 3 #> 3 4 4 #> 4 5 5
不需要调用它,因为您已在代码中自行关闭了模式。您可能不知道该模态已被解雇。您不需要收到解雇信号,因为您首先发出了解雇信号。
通常不会收到委托方法调用来报告您自己的代码所做的事情。委托方法报告用户操作。如果您自己在代码中所做的一切都作为委托方法调用而返回,那就太疯狂了。
答案 1 :(得分:2)
Mojtaba Hosseini,答案是我一直在寻找的东西。
当前,我需要编写一个委托函数,以使演示视图知道用户已解雇了模态加号,并执行了presentationControllerDidDismiss处理程序来进行解雇解雇:
public static void editSpace(String source, String target) {
// Source directory where all the files are there
File dir = new File(source);
File[] directoryListing = dir.listFiles();
// Iterate in each file in the directory
for (File file : directoryListing) {
String childName = file.getName();
String childNameNew = "";
// Iterate in each file name and change every space char to dash char
for (int i = 0; i < childName.length(); i++) {
if (childName.charAt(i) == ' ') {
childNameNew += "-";
} else {
childNameNew += childName.charAt(i);
}
}
// Update the new directory of the child
String childDir = target + "\\" + childNameNew;
// Renaming the file and moving it to a new location
if (!(childNameNew.equals(""))
&& (file.renameTo(new File(childDir)))) {
// If file copied successfully then delete the original file .
file.delete();
// Print message
System.out.println(childName + " File moved successfully to "
+ childDir);
}
// Moving failed
else {
// Print message
System.out.println(childName + " Failed to move the file to "
+ childDir);
}
}
}
我想用相同的方式处理这两个问题,因此Mojtaba的答案对我有用。但是,如果您在self.dismiss完成块内调用,则不会调用presentationControllerDidDismiss,您需要在之前调用它。
我修改了代码以使用“ presentationControllerWillDismiss”(为清楚起见),并在以编程方式在我的模态中将其解雇之前简单地调用了委托,效果很好。
@IBAction func btnDismissTap(_ sender: Any) {
self.dismiss(animated: true, completion: {
self.delegate?.myModalViewDidDismiss()
})
}
现在,我不再需要创建委托函数来处理代码中的模式解除,并且我的滑动处理程序可以处理所有情况。
仅供参考,一旦模式消失,我正在“处理”的工作就是在呈现的UI上进行一些UI清理(取消选择等)。
答案 2 :(得分:1)
正如@matt所提到的,没有必要通知被代表驳回观点的人。因为它已经众所周知。 但如果您需要调用该委托方法,则在关闭视图后应自行自行调用它:
@IBAction func btnDismissTap(_ sender: Any) {
self.dismiss(animated: true) {
presentationController?.delegate?.presentationControllerDidDismiss?(presentationController!)
}
}