Delegate text from TextField between Container Views

别说谁变了你拦得住时间么 提交于 2019-12-11 16:04:25

问题


I'm trying to pass text form one TextField to another TextField. Both of them are located in different ContainerViews. Users can switch between different ContainerViews by Segmented Control (thus, all my ContainerView are located inside of the ViewController with Segmented Control).

I tried to do this with delegation: whenever text in TextFieldOne change, this changing should affect on another textField, so, I tried to use action on TextField in first container with Event "Value Changed":

    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!)
    }
}

And here's my second container, where textField should be changed:

    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
    }
}

I followed the structure from this question, but it didn't work out. What is the problem here?

My main View Controller:

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
}

}

来源:https://stackoverflow.com/questions/45079663/delegate-text-from-textfield-between-container-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!