按钮从另一个文件执行操作(Swift)

时间:2014-11-16 20:32:42

标签: swift uibutton uitextview

我想点击一个UIButton来清除UITextView。我希望按钮操作的代码位于另一个Swift文件中。我该怎么做?

由于

1 个答案:

答案 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"
    }
}