【发布时间】:2014-06-07 06:56:00
【问题描述】:
我不知道我做错了什么。我对编程也很陌生,所以我不太擅长调试。这是一个测试应用程序,因此我可以看到 swift 与应用程序开发的关系。到目前为止,我得到了这个:
class ViewController: UIViewController, UITextViewDelegate {
var textView: UITextView!
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
override func viewDidLoad() {
super.viewDidLoad()
var widthField = self.view.bounds.size.width - 10
var heightField = self.view.bounds.size.height - 69 - 221
var textFieldString: String! = ""
//Set up text field
self.textView = UITextView(frame: CGRectMake(5, 64, widthField, heightField))
self.textView.backgroundColor = UIColor.redColor()
self.view.addSubview(self.textView)
//Set up the New button
var newButtonString: String! = "New Note"
var heightButton = 568 - heightField - 1000
let newButton = UIButton(frame: CGRect(x: 5, y: 5, width: widthField, height: 50)) as UIButton
UIButton.buttonWithType(UIButtonType.System)
newButton.setTitle(newButtonString,forState: UIControlState.Normal)
newButton.backgroundColor = UIColor.redColor()
newButton.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(newButton)
}
func buttonAction() {
println("tapped button")
}
}
当我在 iOS 模拟器中按下按钮时,我收到错误“无法识别的选择器已发送到实例”。该应用程序可以正常打开,但只要按下按钮,它就会崩溃。
【问题讨论】:
-
从这个
"buttonAction:"中删除分号,因为你没有任何buttonAction函数/选择器可以使用一个参数。您的函数使用零参数(),它们肯定是不同的。 -
如果您将目标添加到私有函数中也会发生这种情况 - 如果有一个更强大的类型系统来捕获它会很好。
-
致 OP:如果您看到以下答案之一有帮助并且您的问题已得到解决,请考虑通过点击投票计数器下方的复选标记来接受它。
-
正如有人在之前的答案中所说,对我来说,问题是太多之前的 IBAction 仍然链接到该按钮。 Check this screenshot。在我的 Touch up inside 上有多个 IBAction。只需删除旧的并只保留您使用的那个。
标签: swift