在容器视图之间委托TextField中的文本

时间:2017-07-13 11:45:06

标签: ios swift uitextfield appdelegate

我试图将文本从一个TextField传递到另一个TextField。它们都位于不同的ContainerViews中。用户可以通过分段控制在不同的ContainerView之间切换(因此,我的所有ContainerView都位于具有分段控制的ViewController内部)。

我尝试使用委托执行此操作:每当TextFieldOne中的文本发生更改时,此更改都会影响另一个textField,因此,我尝试在第一个容器中对TextField使用操作,其中包含Event&#34; Value Changed&#34;:< / p>

    protocol AddOneWordViewControllerDelegate: class {
    func changeTexeValue(_ text: String)
}

class AddOneWordViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var oneWordAddWord: UITextField!
    weak var delegate: AddOneWordViewControllerDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.oneWordAddWord.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    @IBAction func addOneWordWordChanged(_ sender: UITextField) {
        guard let delegate = delegate else {
            return
        }
        delegate.changeTexeValue(oneWordAddWord.text!)
    }
}

这是我的第二个容器,应该更改textField:

    class AddTwoWordsViewController: UIViewController, UITextFieldDelegate, AddOneWordViewControllerDelegate {

    @IBOutlet weak var twoWordsAddWord: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.twoWordsAddWord.delegate = self
        let app = UIApplication.shared.delegate! as! AppDelegate
        if let viewControllers = app.window?.rootViewController?.childViewControllers {
            viewControllers.forEach({ (vc) in
                if let cont = vc as? AddOneWordViewController {
                    cont.delegate = self
                }
            })
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    func changeTexeValue(_ text: String) {
        twoWordsAddWord.text = text
    }
}

我按照this问题的结构,但它没有成功。这里有什么问题?

我的主视图控制器:

import UIKit
import Firebase

class WordViewController: UIViewController {

@IBOutlet weak var selectedIndex: UISegmentedControl!
@IBOutlet weak var oneWord: UIView!
@IBOutlet weak var twoWords: UIView!
@IBOutlet weak var threeWords: UIView!
@IBOutlet weak var fourWords: UIView!
var ref: DatabaseReference?


var addOneWordViewController: AddOneWordViewController!
var addTwoWordsViewController: AddTwoWordsViewController!
var addThreeWordsViewController: AddThreeWordsViewController!
var addFourWordsViewController: AddFourWordsViewController!

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "oneWord" {
        addOneWordViewController = segue.destination as! AddOneWordViewController
    } else if segue.identifier == "twoWords" {
        addTwoWordsViewController = segue.destination as! AddTwoWordsViewController
    } else if segue.identifier == "threeWords" {
        addThreeWordsViewController = segue.destination as! AddThreeWordsViewController
    } else if segue.identifier == "fourWords" {
        addFourWordsViewController = segue.destination as! AddFourWordsViewController
    }
}




override func viewDidLoad() {
    super.viewDidLoad()
    ref = Database.database().reference()
    self.oneWord.alpha = 1 // setting the initial view of the first Index of Sigment View to the First Container View
    self.twoWords.alpha = 0
    self.threeWords.alpha = 0
    self.fourWords.alpha = 0
}

@IBAction func showWord(_ sender: UISegmentedControl) { // making one of the four container view vivsible depending on the Index of Segment Controller
    if sender.selectedSegmentIndex == 0 {
        UIView.animate(withDuration: 0.5, animations: {
            self.oneWord.alpha = 1
            self.twoWords.alpha = 0
            self.threeWords.alpha = 0
            self.fourWords.alpha = 0
        })
    } else if sender.selectedSegmentIndex == 1 {
        UIView.animate(withDuration: 0.5, animations: {
            self.oneWord.alpha = 0
            self.twoWords.alpha = 1
            self.threeWords.alpha = 0
            self.fourWords.alpha = 0
        })
    } else if sender.selectedSegmentIndex == 2 {
        UIView.animate(withDuration: 0.5, animations: {
            self.oneWord.alpha = 0
            self.twoWords.alpha = 0
            self.threeWords.alpha = 1
            self.fourWords.alpha = 0
        })
    } else {
        UIView.animate(withDuration: 0.5, animations: {
            self.oneWord.alpha = 0
            self.twoWords.alpha = 0
            self.threeWords.alpha = 0
            self.fourWords.alpha = 1
        })

    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
@IBAction func addWord(_ sender: Any) {
    if selectedIndex.selectedSegmentIndex == 0 {
        guard let text = addOneWordViewController.oneWordAddWord.text, !text.isEmpty else {
            return
        }
        ref?.child("LearnedWords").child(addOneWordViewController.oneWordAddWord.text!).setValue(addOneWordViewController.oneWordAddWord.text)
        presentingViewController?.dismiss(animated: true, completion: nil)
    } else if selectedIndex.selectedSegmentIndex == 1 {
        guard let text = addTwoWordsViewController.twoWordsAddWord.text, !text.isEmpty else {
            return
        }
        ref?.child("LearnedWords").child(addTwoWordsViewController.twoWordsAddWord.text!).setValue(addTwoWordsViewController.twoWordsAddWord.text)
        presentingViewController?.dismiss(animated: true, completion: nil)
    } else if selectedIndex.selectedSegmentIndex == 2 {
        guard let text = addThreeWordsViewController.threeWordsAddWord.text, !text.isEmpty else {
            return
        }
        ref?.child("LearnedWords").child(addThreeWordsViewController.threeWordsAddWord.text!).setValue(addThreeWordsViewController.threeWordsAddWord.text)
        presentingViewController?.dismiss(animated: true, completion: nil)
    } else {
        guard let text = addFourWordsViewController.fourWordsAddWord.text, !text.isEmpty else {
            return
        }
        ref?.child("LearnedWords").child(addFourWordsViewController.fourWordsAddWord.text!).setValue(addFourWordsViewController.fourWordsAddWord.text)
        presentingViewController?.dismiss(animated: true, completion: nil)
    }
}


@IBAction func cancelWord(_ sender: Any) {
    presentingViewController?.dismiss(animated: true, completion: nil) // closing this View
}

}

0 个答案:

没有答案