我想点击一个UIButton来清除UITextView。我希望按钮操作的代码位于另一个Swift文件中。我该怎么做?
由于
答案 0 :(得分:2)
如果您希望按钮操作位于不同的Swift文件中,只需在新文件中为extension
创建一个类ViewController
:
<强> ViewController.swift 强>
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myTextField: UITextField!
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.
}
}
<强> ViewControllerExt.swift 强>
import UIKit
extension ViewController {
@IBAction func coolButton(sender: UIButton) {
println("cool button pressed")
myTextField.text = "cool"
}
}