2016-12-03 58 views
0
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

有人可以帮我解决这个问题?请提问任何问题

+0

做你设置你的日期选择器的框架? –

回答

0

在你的代码datePicker()方法有选择的问题此

datePickerView?.addTarget(self, action: Selector("handlePickerView:"), for: .valueChanged) 

删除上述行并更换该行

datePickerView?.addTarget(self, action: #selector(AddPartyViewController.handlePickerView(sender:)), for: .valueChanged) 

我希望这将有助于你

+0

什么是BViewController? – Eric

+0

对不起,它是AddPartyViewController –

+0

你有没有检查? –