按钮触发的问题

时间:2018-05-05 15:33:19

标签: ios swift

我将数据从secondVC恢复到firstVC并在文本字段中显示。

我的VC是FirstVC,带有文本字段,secondVC带有按钮。

我的VC的流程是当用户在firstVC中点击文本字段(这里是文本字段操作Editing Did Begin)时,第二个VC将打开。然后当按钮点击secondVC然后返回到firstVC时,一些数据和数据将显示在同一文本字段中。

所以上面的一切都很好。

现在我想第二次再次单击文本字段(现在文本字段包含一些数据),然后再次转到secondVC。

问题是现在文本字段包含数据。当我点击它时,由于按钮操作属性Editing Did Begin。它不起作用。

如何处理?

以下是我的代码,

First VC

import UIKit

class firstViewController: UIViewController, UITextFieldDelegate, MyProtocol {

var valueSentFromSecondViewController                   : String?
@IBOutlet weak var myTextField: UITextField!

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



@IBAction func myTextFieldACTIONWhenEditingDidBegin(_ sender: Any) {
    myTextField.isUserInteractionEnabled = false
    let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! secondViewController

    secondVC.delegate = self
    self.navigationController?.pushViewController(secondVC, animated: true)
}

func setResultsAfterEvaluation(valueSent: String) {
    self.valueSentFromSecondViewController = valueSent
    print(valueSentFromSecondViewController!)
    myTextField.text = valueSent
    //print(valueSent)

}
}

第二个VC

import UIKit

protocol MyProtocol {
func setResultsAfterEvaluation(valueSent: String)

}

class secondViewController: UIViewController {
var delegate            :   MyProtocol?
var sentValue           :   String?
override func viewDidLoad() {
    super.viewDidLoad()

}

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

@IBAction func btn(_ sender: Any) {
    sentValue = "Ahtazaz"
    delegate?.setResultsAfterEvaluation(valueSent: sentValue!)
    self.navigationController?.popViewController(animated: true)

}

}

1 个答案:

答案 0 :(得分:0)

我认为这是一种更简单的方法,可以实现您想要实现的目标,因为它不涉及协议和额外功能。如果您有任何问题,请询问。 :)

First View Controller:

import UIKit
//By declaring a variable outside of any class, it is always active in memory and accessible anywhere.
var textFieldText: String = ""

class ViewController1: UIViewController, UITextFieldDelegate {

@IBOutlet weak var textField1: UITextField!

func textFieldDidBeginEditing(_ textField: UITextField) { 
//textField.resignFirstResponder is used to dismiss the keyboard.  By putting it in this function, it hides the keyboard, which prevents users from entering custom text into your text field.
    textField.resignFirstResponder()
    let VC2 = self.storyboard?.instantiateViewController(withIdentifier: "ViewController2") as! ViewController2
    self.navigationController?.pushViewController(VC2, animated: true) 
}

override func viewDidLoad() {
    super.viewDidLoad()
//This links textField1 on your storyboard to the textFieldDidBeginEditing function.
    textField1.delegate = self
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
//Every time the view is about to appear, this is called.  This is where we update the text field's text.
    textField1.text = textFieldText
}
}

第二视图控制器:

import UIKit

class ViewController2: UIViewController {

@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!

//If the data you are trying to pass is the button's title, use these two functions.
@IBAction func button1Tapped(_ sender: UIButton) {
    textFieldText = (button1.currentTitle)!
    self.navigationController?.popViewController(animated: true)
}

@IBAction func button2Tapped(_ sender: UIButton) {
    textFieldText = (button2.currentTitle)!
    self.navigationController?.popViewController(animated: true)
} 

//If the data you are trying to pass is not the button's title, use these.
@IBAction func button1Tapped(_ sender: UIButton) {
    textFieldText = "Your Text Here"
    self.navigationController?.popViewController(animated: true)
}

@IBAction func button2Tapped(_ sender: UIButton) {
    textFieldText = "Your Text Here"
    self.navigationController?.popViewController(animated: true)
}
}