此代码允许您使用datePicker为
Xcode 7更新textField,但是当我尝试在Xcode 8和
Swift 3中实现它时,应用程序崩溃并在AppDelegate.swift文件中抛出SIGABRT错误.我检查了所有的@IB,并且没有任何需要删除的杂项连接.任何指导将不胜感激.
import UIKit class ViewController: UIViewController,UITextFieldDelegate { @IBOutlet var eventStartText: UITextField! override func viewDidLoad() { super.viewDidLoad() eventStartText.delegate = self // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: TextField Delegate func datePickerChanged(sender: UIDatePicker) { let formatter = DateFormatter() formatter.dateStyle = .full eventStartText.text = formatter.string(from: sender.date) print("Try this at home") } func textFieldDidBeginEditing(_ textField: UITextField) { let datePicker = UIDatePicker() textField.inputView = datePicker datePicker.addTarget(self,action: (Selector(("datePickerChanged:"))),for: .valueChanged) print("This Worked") } func textFieldShouldReturn(_ textField: UITextField) -> Bool { eventStartText.resignFirstResponder() return true } // MARK: Helper Methods func closekeyboard() { self.view.endEditing(true) } // MARK: Touch Events override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) { closekeyboard() } /* // MARK: - Navigation // In a storyboard-based application,you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue,sender: Any?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ }
解决方法
问题在于选择器语法,从Swift 3中你需要在selector中指定第一个参数名,所以改变你的选择器语法.
datePicker.addTarget(self,action: #selector(datePickerChanged(sender:)),for: .valueChanged)