import UIKit
class AddPartyViewController: UIViewController, UITextFieldDelegate
{
@IBOutlet weak var textName: UITextField!
@IBOutlet weak var textAddress: UITextField!
@IBOutlet weak var textDate: UITextField!
var date:NSDate?
var datePickerView: UIDatePicker?
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
datePicker()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func buttonAddParty_Click(sender: UIButton)
{
party.addParty(name: textName.text!, address:textAddress.text!)
self.view.endEditing(true)
textName.text = ""
textAddress.text = ""
}
func datePicker()
{
datePickerView = UIDatePicker()
datePickerView?.datePickerMode = UIDatePickerMode.date
datePickerView?.addTarget(self, action: Selector("handlePickerView:"), for: .valueChanged)
textDate.inputView = datePickerView
}
func handlePickerView(sender: UIDatePicker)
{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM dd, yyy"
date = sender.date as NSDate?
textDate.text = dateFormatter.string(from: sender.date)
}
}
以上就是添加待办事项列表代码及以下的tableview代码添加的UIDatePicker后,它打印错误
import UIKit
class PartiesTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
@IBOutlet var tblLists: UITableView!
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.
}
//returning to view
override func viewWillAppear(_ animated: Bool)
{
tblLists.reloadData()
}
//UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return party.lists.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "test")
cell.textLabel?.text = party.lists[indexPath.row].name
//cell.textLabel?.text = party.lists[indexPath.row].date
cell.detailTextLabel?.text = party.lists[indexPath.row].address
return cell
}
//UITableViewDelegate - delete
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
if(editingStyle == UITableViewCellEditingStyle.delete)
{
party.lists.remove(at: indexPath.row)
tblLists.reloadData()
}
}
}
我与斯威夫特3.待办事项列表上的工作是什么,我试图做的是增加名称,地址和日期,方法是在AddPartyViewController中输入textField。名称和地址是字符串,但日期是来自UIDatePicker的日期。但问题是,当我点击日期选择,选择在模拟器的UIDatePicker一个日期,它给出了这样的错误:
libc++abi.dylib: terminating with uncaught exception of type NSException
有人可以帮我解决这个问题?请提问任何问题
做你设置你的日期选择器的框架? –