Swift方法不会覆盖其超类中的任何方法

时间:2016-08-11 08:19:33

标签: iphone swift swift2

我对Swift相对较新,我正在学习一些基本的教程,但我似乎遇到了一些问题,试图让用户按下返回以最小化键盘或点击键盘,键盘将消失,我理解为什么我收到这些错误但不知道如何修复它,我觉得在我使用的较新版本的Swift中可能已经改变了一些东西,因为他使用的是比我更旧的版本,任何人都可能解释如何解决这两个错误呢?任何帮助将非常感谢这里是我的源代码:(第一个错误,类型'viewController'的值没有成员'文本',其次,touchesBegan方法不会覆盖其超类中的任何方法)

import UIKit

class ViewController: UIViewController {

@IBAction func buttonPressed(sender: AnyObject) {

   label.text = textArea.text

}

@IBOutlet weak var textArea: UITextField!

@IBOutlet weak var label: UILabel!

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

    self.text.delegate = self

}

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

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    self.view.endEditing(true)
}

func textFieldShouldReturn(textField: UITextField!) -> Bool {

    textField.resignFirstResponder()

    return true

}

}

2 个答案:

答案 0 :(得分:3)

根据您发布的图片,您有2个问题:

1)方法touhesBegan您正在使用is not correct

纠正一个:

func touchesBegan(_ touches: Set<UITouch>, withEvent event: UIEvent?)

此致:

func touchesBegan(touches: NSSet, withEvent event: UIEvent)

我认为你想要一个UITextField的委托,所以这个不是corerct:touchesBegan是UIReponder委托的方法而不是UITextFieldDelegate。

Here您可以找到UITextFieldDelegate的参考资料。

2)变量text在您的代码中不存在。我想你想改用textArea

希望这可以帮助你,快乐的编码!

答案 1 :(得分:2)

在您的情况下更改以下内容:

而不是:

self.text.delegate = self

改变:

self.textArea.delegate = self


override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    }

代表添加像这样

class ViewController: UIViewController, UITextFieldDelegate {
}