【发布时间】:2015-07-29 16:43:43
【问题描述】:
我想在 ViewController 中触摸一个按钮并在自定义类中运行一个简单的函数。不需要参数。代码基本上是:
class MyClass: UIView {
//init function is here
//other setup stuff
...
func SetFocus() {
//I want to set the camera focus to infinity.
var error: NSErrorPointer = nil
var pos: CFloat = 1.0
captureDevice!.lockForConfiguration(error)
captureDevice!.setFocusModeLockedWithLensPosition(pos, completionHandler: nil)
captureDevice!.unlockForConfiguration()
}
}
and in ViewController
import UIKit
class ViewController: UIViewController {
@IBOutlet var myClass: MyClass!
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.
}
@IBAction func myButton(sender: UIButton) {
myClass.SetFocus()
}
}
我已经尝试了所有我能想到的东西(Swift 的新功能),但应用程序要么崩溃,要么我调用的函数无法运行。 MyClass 中的其他一切工作正常。
【问题讨论】:
-
myClass设置在哪里?拨打MyFunction时是否存在? -
如果是
IBOutlet,您是否已将其连接到 Storyboard 中? -
能否在myFunction()中查看具体代码
-
你也可以使用 NSNotificationCenter 来解决这个问题