【发布时间】:2018-01-28 14:20:06
【问题描述】:
这是我的代码:
func didSelectLabel() {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 30))
label.center = viewForEdit.center
label.textAlignment = .center
label.isUserInteractionEnabled = true
func userDragged(gesture: UIPanGestureRecognizer) {
let loc = gesture.location(in: self.viewForEdit)
label.center = loc
}
gesture = UIPanGestureRecognizer(target: self, action: userDragged(gesture:))
label.addGestureRecognizer(gesture)
let alert = UIAlertController(title: "Write your text", message: "", preferredStyle: .alert)
let continueAction = UIAlertAction(title: "Continue", style: .default) { (UIAlertAction) in
label.text = alert.textFields?[0].text
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addTextField { (textField) in
textField.placeholder = "Write here"
}
alert.addAction(continueAction)
alert.addAction(cancelAction)
present(alert, animated: true, completion: nil)
self.viewForEdit.addSubview(label)
}
我想使用
func userDragged(手势: UIPanGestureRecognizer)
作为
的选择器gesture = UIPanGestureRecognizer(target: self, action: userDragged(gesture:))
问题是如果我运行代码它会崩溃说
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[TestApp.ViewControllerEditor userDragged:]:无法识别的选择器发送到实例 0x7fc6b3c282b0”
P.S. 我不能将函数“userDragged(gesture:)”放在函数“didSelectLabel()”之外,因为如果这样做,“label.center = loc”会返回错误。这是因为在函数 'didSelectLabel()' 内部调用了 'label'
【问题讨论】: