当我关闭一个模态视图控制器时,我希望tableview更新,我在iPad上使用表单表单呈现样式,所以viewWillAppearviewDidAppear方法将不起作用

推荐答案

我觉得segue方法更优雅.

假设我们有ViewControllerAViewControllerB.我们在ViewControllerB,我们希望从ViewControllerB直接跳回到ViewControllerA,并在ViewControllerA中更新表视图.

ViewControllerA中,在ViewController类中添加以下操作:

@IBAction func unwindToViewControllerA(segue: UIStoryboardSegue) {
    DispatchQueue.global(qos: .userInitiated).async {
        DispatchQueue.main.async {
            self.tableView.reloadData()
        }
    }
}

Yes, this code goes in the ViewController of the ViewController you want to go back to!

现在,您需要从ViewControllerB的故事板(StoryboardB)中创建一个退出序列.继续,打开StoryboardB, Select 故事板.按住CTRL键并拖动以退出,如下所示:

enter image description here

您将获得一个可供 Select 的序列列表,包括我们刚刚创建的序列:

enter image description here

你现在应该有一个segue,点击它:

enter image description here

进入inspector并设置一个唯一的id:

ViewControllerB中,在您想要退出并返回ViewControllerA的位置,执行以下操作(使用我们之前在inspector中设置的id):

self.performSegue(withIdentifier: "yourIdHere", sender: self)

现在,无论何时使用segue返回到ViewControllerAViewControllerA都会立即更新TableView.

Swift相关问答推荐

如何更新Square Order?

使用SWIFT宏从另一个枚举添加 case

从后台线程获取@发布属性的值(不更改它)是正确的吗?

从Swift中的泛型类继承?

NavigationLink和ObservableObject的动画片段

Swift // Sprite Kit:类别位掩码限制

为什么 id 不能通过 struct 从 Objective-C 移植到 Swift?

如何在填充上添加 if else 语句? SwiftUI

Vapor 4,如何按外键过滤?

如何在 SwiftUI 的 fileImporter 中为 allowedContentTypes 设置 xcodeproj 类型?

.onTapGesture 不适用于 Stepper,仅适用于其文本

获取 SwiftUI 中 Binding 的值更改更新

判断 Swift 程序是否正在输出到终端

iOS Swift - 如何更改表格视图的背景 colored颜色 ?

Type 应该采用什么协议来让泛型函数将任何数字类型作为 Swift 中的参数?

如何在Swift中找出字母是字母数字还是数字

Swift中字节数组的NSData

如何在 SwiftUI Segmented Picker 中更改选定的段 colored颜色

Swift 2.0 按属性对对象数组进行排序

Swift:长按手势识别器 - 检测轻按和长按