ios - 在弹出 segue 为 'dismissed' 后刷新父 View 上的 IBOutlet 外观

标签 ios swift3 segue

我有一个由主屏幕和“设置”屏幕(弹出模式)组成的小应用程序。我使用 iOS segue 加载设置弹出窗口。

在设置弹出窗口中,您基本上将背景设置为浅色或深色,但我不确定在模式被关闭后如何加载我的“makeScreenLight”方法。

我是否使用 viewDidAppear,如何调用它?

或者我是在关闭弹出窗口之前或期间将它与 dismiss 方法一起使用:

@IBAction func BackFromSettings(_ sender: AnyObject) {
    dismiss(animated: true, completion: nil)
}

最佳答案

有多种方法可以实现这一点,包括全局状态、通知和委派。我将举一个使用委托(delegate)的例子:

首先,我们将创建一个定义委托(delegate)关系的协议(protocol)。

protocol SecondViewControllerDelegate: class {
    func settingsUpdated(light: Bool)
}

接下来,我们将使第一个 View Controller 符合这个新协议(protocol),并实现它的一个方法。我们还将确保在我们进行 segue 时将第二个 View Controller 的委托(delegate)设置为第一个 View Controller 。

class FirstViewController: UIViewController, SecondViewControllerDelegate {

    func settingsUpdated(light: Bool) {
        // modify the UI here
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        guard let secondViewController = segue.destination as? SecondViewController else { return }
        secondViewController.delegate = self
    }

}

当第二个 View Controller 中发生 Action 时(在本例中是按下开关),我们可以在我们的委托(delegate)上调用该方法。这将在第一个 View Controller 中进行更改。

class SecondViewController: UIViewController {

    weak var delegate: SecondViewControllerDelegate?

    @IBAction func buttonPressed(_ sender: UISwitch) {
       delegate?.settingsUpdated(light: sender.isOn)
    }

    @IBAction func backFromSettings(_ sender: AnyObject) {
        dismiss(animated: true, completion: nil)
    }

}

关于ios - 在弹出 segue 为 'dismissed' 后刷新父 View 上的 IBOutlet 外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41108416/

相关文章:

ios - Swift 3 Realm 在 IOS 10 模拟器上崩溃

swift - 计算 UITextField 的字符因 NSException 崩溃

ios - 使用泛型设置 cellDelegate

ios - 停止使用带有证书的应用程序

ios - 全局初始化变量在 Swift UIView XIB 中更改为 'nil'?

html - 对象/iframe 溢出滚动在 ios (cordova) 中不起作用

ios - 在一个 UIViewController 中实现对多个 UITableView 的 peek 和 pop

ios - 快速地将 segue 中的项目持续添加到数组中

ios - 不同的 View Controller segue

android - 在没有应用程序的情况下测试 firebase 云消息传递