self.dismiss сбрасывает данные экрана родительского контроллера - PullRequest
0 голосов
/ 19 сентября 2019

Я представляю контроллер представления внутри ParentViewController:

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)   
    }
}

Как я могу сохранить исходное состояние родительского контроллера представления после вызова dismiss?

1 Ответ

0 голосов
/ 19 сентября 2019

После нескольких часов копаний документации Apple, наконец, я нашел следующее решение, и оно работает для меня.Надеюсь, что это поможет кому-то в будущем.

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

presentViewController.modalPresentationStyle = .overCurrentContext

Кроме того, если есть панель вкладок, нижняя часть дочернего контроллера представления будет под ним, если вы установитестиль представления до overCurrentContext .Обходной путь - представить дочерний контроллер представления из rootViewController окна.

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

Теперь при вызове dismiss viewWillAppear не запускается, и я могу сохранить данные родительского контроллера представления и его состояние.

...