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
有人可以幫我解決這個問題?請提問任何問題
做你設置你的日期選擇器的框架? –