gpt4 book ai didi

swift - self.dismiss 重置父 View Controller 的屏幕数据

转载 作者:行者123 更新时间:2023-11-30 10:36:02 26 4
gpt4 key购买 nike

我在 ParentViewController 中呈现一个 View Controller :

let item = getCurrentItem()
let presentViewController = viewController as! TestViewController
presentViewController.id = item.id
presentViewController.dismissController { // my custom delegate to inform the parent vc
self.dismiss(animated: true, completion: nil) // presenting view controller is closing then viewWillAppear will be triggered
}

self.present(presentViewController, animated: true, completion: nil)

但是当调用dismiss时,ParentViewController的全局数组和变量会恢复到它们的初始值。

class ParentViewController {

// MARK: - Stored Properties

private let refreshControl = UIRefreshControl()
private var items = [String:[Item]]()
private var screenLoaded: Bool = false

// MARK: - Controller Lifecycle

override func viewDidLoad() {
super.viewDidLoad()
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

// after dismiss is called viewWillAppear gets hit but the "screenLoaded" is always false
if !screenLoaded {
screenLoaded = true
}

// items is initialised as well
}

private func openPopover() {
let item = getCurrentItem()
let presentViewController = viewController as! TestViewController
presentViewController.id = item.id
presentViewController.dismissController { // my custom delegate to inform the parent vc
self.dismiss(animated: true, completion: nil) // presenting view controller is closing then viewWillAppear will be triggered
}

self.present(presentViewController, animated: true, completion: nil)
}
}

调用关闭后如何保持父 View Controller 的原始状态?

最佳答案

经过几个小时的挖掘苹果文档,我终于找到了以下解决方案,它对我有用。希望它对将来的人有所帮助。

[https://developer.apple.com/documentation/uikit/uimodalpresentationstyle/overcurrentcontext][1]

presentViewController.modalPresentationStyle = .overCurrentContext

此外,如果有选项卡栏,如果将呈现样式设置为 overCurrentContext,则 subview Controller 的底部将位于其下方。解决方法是从窗口的 rootViewController 中呈现 subview Controller 。

sender.view.window?.rootViewController?.present(viewController, animated: true, completion: nil)

现在,当调用解除时,viewWillAppear 不会被触发,我可以保留父 View Controller 的数据及其状态。

关于swift - self.dismiss 重置父 View Controller 的屏幕数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58010072/

26 4 0
文章推荐: java - 返回类型 ResponseEntity 和 ResponseEntity 和 ResponseEntity 有什么区别