简体   繁体   English

如何将值发送到Swift中的父视图控制器

[英]How to send values to a parent view controller in Swift

I am working on a settings view controller screen for my iOS app written in swift. 我正在使用swift编写的iOS应用程序的设置视图控制器屏幕。 I am using a navigation controller to run the main settings table view which shows the cell titled, "Input Method." 我正在使用导航控制器来运行主设置表视图,该视图显示标题为“输入方法”的单元格。 The current method is listed on the right of the cell. 当前方法列在单元格的右侧。 They can click the cell to go to the next view controller where they can select the input method that they'd like. 他们可以单击单元格转到下一个视图控制器,在那里他们可以选择他们喜欢的输入法。

From here, there are two sections. 从这里开始,有两个部分。 The first is the input method to choose (touchscreen or joystick). 第一种是选择的输入法(触摸屏或操纵杆)。 The second section is joystick specific on whether or not the person is a lefty or righty. 第二部分是关于该人是否是左撇子或右撇子的操纵杆。 I don't want to have the vc unwind when they choose one box because they may choose one in another section too. 当他们选择一个盒子时,我不想让vc放松,因为他们也可以在另一个盒子中选择一个。

My question: How can I update the text field in the parent controller from the child controller. 我的问题:如何从子控制器更新父控制器中的文本字段。

Problems I'm having for optional solutions: 我对可选解决方案的问题:

let parentVC: UIViewController = (self.navigationController?.parentViewController)!
parentVC.inputMethod.text? = cellSelected // This doesn't work because it cannot find the label inputMethod.

viewDidLoad() will cause a lag and the user sees the old method before it changes. viewDidLoad()将导致延迟,并且用户在更改之前会看到旧方法。

I cannot find out how to run a segue when someone clicks the back button at the upper left hand side in the navigation controller, since the navigation controller controls the segue. 当有人点击导航控制器左上角的后退按钮时,我无法找到如何运行segue,因为导航控制器控制了segue。

It is not a good idea to cast the parent view controller, even when you are sure which class represents. 即使您确定哪个类表示,也不应该转换父视图控制器。 I'll do it with a protocol: 我会用协议做的:

In the child controller add a protocol like: 在子控制器中添加如下协议:

protocol ChildNameDelegate {
    func dataChanged(str: String)
}

class ChildClass {

    weak var delegate: ChildNameDelegate?

    func whereTheChangesAreMade(data: String) {
        delegate?.dataChanged(data)
    }
}

And in the parent: 而在父母:

class ParentClass: ChildNameDelegate {

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        guard let segueId = segue.identifier else { return }

        switch segueId {
        case "childSegue":
            let destVC = segue.destinationViewController as! ChildClass
            destVC.delegate = self
            break
        default:
            break
        }
    }

    // Child Delegate
    func dataChanged(str: String) {
        // Do whatever you need with the data
    }
}

You need to cast the parentViewController to whatever custom class it has. 您需要将parentViewController为它具有的任何自定义类。 For example, if the parent has the class ExampleParentController , you would write: 例如,如果父类具有ExampleParentController类,则应编写:

let parentVC = (self.navigationController?.parentViewController)! as! ExampleParentController
parentVC.inputMethod.text? = cellSelected

I found a solution here: Modifing one variable from another view controller swift http://www.raywenderlich.com/115300/swift-2-tutorial-part-3-tuples-protocols-delegates-and-table-views 我在这里找到了一个解决方案: 从另一个视图控制器swift修改一个变量 http://www.raywenderlich.com/115300/swift-2-tutorial-part-3-tuples-protocols-delegates-and-table-views

Instead of trying to access the view controller directly (which would be easier if it weren't returning a nil for the view controller) you can use a delegate method to adjust the variables. 而不是试图直接访问视图控制器(如果没有为视图控制器返回nil会更容易),您可以使用委托方法来调整变量。 The delegate worked like a charm! 代表工作就像一个魅力!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM