ios - 如何从另一个 UIViewController 更改分段控件标题(设置)

标签 ios swift

编辑 Current Code

所以我刚刚开始 iOS 开发,所以我决定制作一个小费计算器来学习 Swift 的基础知识和基础。我感觉已经完成了 70%,但是我在引用另一个 UIViewController 的数据时遇到了麻烦...

This is what my story board looks like.....

所以我有加载屏幕,单击按钮后它会转到小费计算器...然后我有一个可以更改分段控制部分的标题的设置。因此,如果我想将默认设置更改为(10%、15%、30%),我可以在“设置”中执行此操作。但是我似乎无法弄清楚如何获取 TextField.text 并将其作为分段控件标题的标题。

This is the code for the Tip Calculator (2nd screen)

我有一个属于“设置”页面的 ViewController3 类,但它完全是空白的。

最佳答案

要将设置传递回您的第一个 viewController,您可以使用从第一个 viewController 传递的闭包。

以下是如何执行此操作的示例。在 prepare(for:) 中,将闭包 processTipSettings 设置为指向处理程序函数。在这种情况下,changeSegmentLabels 将采用提示设置并使它们成为分段控件的标签。

当用户点击“保存”时,按钮的@IBAction将使用textFieldLabels中的值调用processTipSettings,并且changeSegmentLabels将更新分段控件中的标题。

<小时/>

ViewController.swift:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var tipsSegmentedController: UISegmentedControl!

    func changeSegmentLabels(labels: [String?]) {
        if labels.count == 3 {
            for i in 0..<3 {
                tipsSegmentedController.setTitle(labels[i], forSegmentAt: i)
            }
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "settings" {
            if let dvc = segue.destination as? SettingsViewController {
                dvc.processTipSettings = changeSegmentLabels
            }
        }
    }

}
<小时/>

SettingsViewController.swift

class SettingsViewController: UIViewController {

    @IBOutlet weak var tip1: UITextField!
    @IBOutlet weak var tip2: UITextField!
    @IBOutlet weak var tip3: UITextField!

    // Call this closure when Save is pressed.
    var processTipSettings: (([String?]) -> ())?

    @IBAction func saveSettings(_ sender: UIButton) {
        processTipSettings?([tip1.text, tip2.text, tip3.text])

        _ = self.navigationController?.popViewController(animated: true)
    }

}

关于ios - 如何从另一个 UIViewController 更改分段控件标题(设置),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40832792/

相关文章:

ios - 从图像数组创建动画

ios - 如何从 iOS-Objective-c 中的联系人框架获取生日和周年纪念标签

ios - 如何将手机的所有远程(推送)通知重新安排到特定时间?在iOS中有可能吗?

ios - 获取任何包含 ID 核心数据的数据

swift - 由于 'internal' 保护级别,“值”无法访问

ios - 多个 MKPointAnnotations 上的自定义图像

javascript - 如何在 Apple Watch 中显示 D3.js 图表?

ios - 奇怪的 Swift 的#selector 行为。通过#selector 参数的值完全不同

ios - 退格键不能在正则表达式之外快速工作

ios - React Native 中的全屏图像