ios - 在父 View Controller 中点击按钮时容器 View 丢失 textField 值

标签 ios swift uitextfield uisegmentedcontrol

我有一个带有 UISegmentedControlUIButton 的 viewController。 在这个 viewController 中,我有两个容器,每个容器包含一个 viewController,里面有一个 UITextField

enter image description here

我想在单击按钮时将值保存在文本字段中。

这是我到目前为止编写的代码:

View Controller :

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        //
        // 

        containerA.showView()
        containerB.hideView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func buttonTapped(sender: UIButton) {
        print(ContainerAViewController.sharedInstance.textFieldA)


    }
    @IBAction func segmentedControlValueChanged(sender: AnyObject) {
        switch(sender.selectedSegmentIndex) {
        case 0 : containerA.showView()
            containerB.hideView()

        case 1 : containerB.showView()
        containerA.hideView()

        default : containerA.showView()
        containerB.hideView()

        }
    }

    @IBOutlet weak var containerA: UIView!
    @IBOutlet weak var containerB: UIView!


    func hideView(view: UIView) {
        view.userInteractionEnabled = false
        view.hidden = true
    }

    func showView(view: UIView) {
        view.userInteractionEnabled = true
        view.hidden = false
    }

}


extension UIView {
    func hideView() -> UIView {
        self.userInteractionEnabled = false
        self.hidden = true
        return self
    }

    func showView() -> UIView {
        self.userInteractionEnabled = true
        self.hidden = false
        return self
    }

}

ContainerAViewController:

class ContainerAViewController: UIViewController {

    @IBOutlet weak var textFieldA: UITextField!

    static let sharedInstance = ContainerAViewController()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
}

ContainerBViewController:

class ContainerBViewController: UIViewController {

    @IBOutlet weak var textFieldB: UITextField!

    static let sharedInstance = ContainerBViewController()


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
}

当我点击按钮时,出现以下错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

有人可以帮忙吗?

最佳答案

你不应该试图操纵另一个 View Controller 的 View 。这违反了封装原则,这是面向对象开发中的一个重要原则。

您应该为您的 subview Controller (ContainerAViewController 和 ContainerBViewController)提供字符串属性,并让这些 View Controller 的代码在用户将文本输入到 View Controller 的文本字段时设置该字符串属性。

忽略该设计缺陷,您的代码就没有意义。您将 CLASS 显示为 ContainerAViewController,但您的 buttonTapped 方法试图向 ContainerAViewController 单例询问文本字段的值。这没有意义。

您想在父 View Controller 中拥有指向 subview Controller 的属性。

您应该在父 View Controller 中实现一个 prepareForSegue 方法,并在该 prepareForSegue 方法中查找在加载 subview Controller 时触发的嵌入转场。当发生这种情况时,您应该设置指向 subview Controller 的属性。

关于ios - 在父 View Controller 中点击按钮时容器 View 丢失 textField 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37307992/

相关文章:

ios - keytoolbar 上的下一个按钮将最后一个字段设置为第一响应者,

ios - getter setter 模型类 Oop

ios - 在 Sprite Kit 中确定一个点是否位于物理体的边界内

ios - 当文本长度 <=4 时,如何禁用文本字段的 UIKeyboardType 默认值上的默认返回键

php - 如何快速使用现有的 php/mysql 散列密码

ios - 使用对象属性填充 TableView ?

ios - 点击字段时 zoomScaled UIScrollView 定位不正确

IOS/swift : Parse JSON data and dictionary

ios - Facebook 登录卡住

swift - SRWebClient 图片上传无法转换值