我想在editText中调用textFieldShouldReturn函数,但我不知道怎么做,我该怎么办?我尝试在editText函数中复制孔函数,但它没有工作。 editText函数识别正在单击的按钮,并且可能会更改为textFieldShouldReturn中定义的标签单击的按钮的标签,但它不起作用。
ViewController with edit
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var textFieldInput: UITextField!
@IBOutlet weak var iphoneSaveCharName: UIButton!
@IBOutlet weak var charOne: UIButton!
@IBOutlet weak var charTwo: UIButton!
@IBOutlet weak var charThree: UIButton!
@IBOutlet weak var charFour: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
var savedText: String!
var tempbutton: UIButton!
// func textFieldShouldReturn(textFieldInput: UITextField) -> Bool {
// savedText = textFieldInput.text
// textFieldInput.resignFirstResponder()
// return false
// }
@IBAction func editText(sender: AnyObject) {
if sender is UILongPressGestureRecognizer &&
sender.state == UIGestureRecognizerState.Began {
textFieldInput.hidden = false
var tempbutton = sender.view as UIButton
textFieldEditCompleted(textFieldInput)
if tempbutton.tag == 1 {
charOne.setTitle(savedText, forState: .Normal)
} else if tempbutton.tag == 2{
charTwo.setTitle(savedText, forState: .Normal)
} else if tempbutton.tag == 3{
charThree.setTitle(savedText, forState: .Normal)
} else if tempbutton.tag == 4{
charFour.setTitle(savedText, forState: .Normal)
}
}
}
func textFieldEditCompleted(textFieldInput: UITextField) -> Bool
{
savedText = textFieldInput.text
textFieldInput.resignFirstResponder()
tempbutton.setTitle(savedText, forState: .Normal)
return false
}
func textFieldShouldReturn(textFieldInput: UITextField) -> Bool
{
return textFieldEditCompleted(textFieldInput);
}
}
更新2:Behavior
答案 0 :(得分:0)
你是怎么称呼这个功能的?
同样调用委托方法不是一个好习惯,你不应该这样做。
而不是写一个像:
这样的函数func textFieldEditCompleted(textFieldInput: UITextField) -> Bool
{
savedText = textFieldInput.text
textFieldInput.resignFirstResponder()
return true
}
func textFieldShouldReturn(textFieldInput: UITextField) -> Bool
{
return textFieldEditCompleted(textFieldInput);
}
您可以从textFieldEditCompleted
和textFieldShouldReturn
拨打editText
功能。