Swift 如何将数据传递到AddController到UITableViewController
我的swift代码有问题。我有一个UITableViewCotroller和一个原型单元格,在这个单元格中有三个标签。我用@IBOutlet为标签创建了文件TimerCell.swift:Swift 如何将数据传递到AddController到UITableViewController,swift,Swift,我的swift代码有问题。我有一个UITableViewCotroller和一个原型单元格,在这个单元格中有三个标签。我用@IBOutlet为标签创建了文件TimerCell.swift: import UIKit class TimerCell: UITableViewCell { @IBOutlet var circostanza: UILabel! @IBOutlet var luogo: UILabel! @IBOutlet var tempo: UILabel! overrid
import UIKit
class TimerCell: UITableViewCell {
@IBOutlet var circostanza: UILabel!
@IBOutlet var luogo: UILabel!
@IBOutlet var tempo: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
以及带有以下代码的文件TimerModel.swift:
import UIKit
class TimerModel: NSObject, NSCoding {
var circostanza :String!
var luogo : String!
var tempo : String!
init(circostanzaIn:String, luogoIn:String, tempoIn:String) {
circostanza = circostanzaIn
luogo = luogoIn
tempo = tempoIn
}
internal required init(coder aDecoder: NSCoder) {
self.circostanza = aDecoder.decodeObjectForKey("circostanza") as! String
self.luogo = aDecoder.decodeObjectForKey("luogo") as! String
self.tempo = aDecoder.decodeObjectForKey("tempo") as! String
}
func encodeWithCoder(encoder: NSCoder) {
encoder.encodeObject(self.circostanza, forKey: "circostanza")
encoder.encodeObject(self.luogo, forKey: "luogo")
encoder.encodeObject(self.tempo, forKey: "tempo")
}
}
然后,我在UITableViewCotroller中有一个+按钮,用于打开一个带有三个文本字段的AddController,以添加一些数据。我想将这些数据保存在单元格的标签中。添加控制器中的代码为:
import UIKit
class AddTimerController: UIViewController, UITextFieldDelegate {
@IBOutlet var fieldCircostanza: UITextField!
@IBOutlet var fieldLuogo: UITextField!
@IBOutlet var fieldTempo: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
fieldCircostanza.delegate = self
fieldLuogo.delegate = self
//
var keyboardToolbar = UIToolbar(frame: CGRectMake(0, 0, self.view.bounds.size.width, 44))
keyboardToolbar.barStyle = UIBarStyle.BlackTranslucent
keyboardToolbar.backgroundColor = UIColor.redColor()
keyboardToolbar.tintColor = UIColor.whiteColor()
var flex = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
var save = UIBarButtonItem(title: "Fatto", style: UIBarButtonItemStyle.Done, target: fieldTempo, action: "resignFirstResponder")
keyboardToolbar.setItems([flex, save], animated: false)
fieldTempo.inputAccessoryView = keyboardToolbar
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder() // chiudere la tastiera nei campi di testo
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func annulla(sender: UIButton) {
dismissViewControllerAnimated(true, completion: nil) // chiude una modal
}
@IBAction func salva(sender: UIButton) {
if fieldCircostanza.text.isEmpty &&
fieldLuogo.text.isEmpty &&
fieldTempo.text.isEmpty{
//alertView
return
}
var timer = TimerModel(circostanzaIn: fieldCircostanza.text,
luogoIn: fieldLuogo.text,
tempoIn: fieldTempo.text)
DataManager.sharedInstance.salvaArray()
DataManager.sharedInstance.detail.myCollection.reloadData()
dismissViewControllerAnimated(true, completion: nil)
}
}
最后是UITableViewCotroller文件:
import UIKit
class TimerViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return DataManager.sharedInstance.storage.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TimerCell
//cell.circostanza.text = timer.circostanza
//cell.luogo.text = timer.luogo
//cell.tempo.text = timer.tempo
return cell
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
DataManager.sharedInstance.storage.removeAtIndex(indexPath.row)
DataManager.sharedInstance.salvaArray()
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
// MARK: - Navigazione
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "modifica" {
//let controller = (segue.destinationViewController as UINavigationController).topViewController as AddController
}
}
}
您能帮助我保存数据的代码吗?要访问tableViewController中的计时器,请执行以下操作:
import UIKit
class AddTimerController: UIViewController, UITextFieldDelegate {
@IBOutlet var fieldCircostanza: UITextField!
@IBOutlet var fieldLuogo: UITextField!
@IBOutlet var fieldTempo: UITextField!
var timer: TimerModel! // Made timer a class member
override func viewDidLoad() {
......
}
// your code ....
@IBAction func salva(sender: UIButton) {
if fieldCircostanza.text.isEmpty &&
fieldLuogo.text.isEmpty &&
fieldTempo.text.isEmpty{
//alertView
return
}
timer = TimerModel(circostanzaIn: fieldCircostanza.text, // intialize timer
luogoIn: fieldLuogo.text,
tempoIn: fieldTempo.text)
DataManager.sharedInstance.salvaArray()
DataManager.sharedInstance.detail.myCollection.reloadData()
dismissViewControllerAnimated(true, completion: nil)
}
}
然后在tableViewController中,按如下方式访问它:
class TimerViewController: UITableViewController {
var timer: TimerModel! // Change to this
override func viewDidLoad() {
super.viewDidLoad()
var controller : AddTimerController = AddTimerController()
timer = controller.timer
}
// your code .......
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TimerCell
cell.circostanza.text = timer.circostanza
cell.luogo.text = timer.luogo
cell.tempo.text = timer.tempo
return cell
}
}
对于您在注释中的查询,这将更正: 在您的TimerModel中:
import UIKit
class TimerModel: NSObject, NSCoding {
var circostanza :String!
var luogo : String!
var tempo : String!
override init() { // add this method
super.init()
}
init(circostanzaIn:String, luogoIn:String, tempoIn:String) {
circostanza = circostanzaIn
luogo = luogoIn
tempo = tempoIn
}
internal required init(coder aDecoder: NSCoder) {
self.circostanza = aDecoder.decodeObjectForKey("circostanza") as! String
self.luogo = aDecoder.decodeObjectForKey("luogo") as! String
self.tempo = aDecoder.decodeObjectForKey("tempo") as! String
}
func encodeWithCoder(encoder: NSCoder) {
encoder.encodeObject(self.circostanza, forKey: "circostanza")
encoder.encodeObject(self.luogo, forKey: "luogo")
encoder.encodeObject(self.tempo, forKey: "tempo")
}
}
那么,您遇到了什么错误?嗨,在AddController中,当我单击“保存”按钮时,我在文本字段中添加的数据没有保存在UITableViewController上的单元格标签中。好的,因此您没有遇到任何崩溃或错误,只是所需标签中的值没有更新??是,我认为UITableViewController中缺少一些保存数据的代码,但我不知道如何设置它。我只看到了原型单元格,当我尝试添加新数据时,没有显示任何内容。您已经测试了-timer.circostanza-timer.luogo-timer.tempo是否有值,方法是在将这些值分配给单元格标签之前在tableViewController中打印这些值感谢您的可用性,但是现在我出现了一个错误:无法在TimerViewController文件的第行调用没有参数的类型“TimerModel”的初始值设定项:
var timer:TimerModel=TimerModel()
Hi,因为在TimerModel中只提供init函数,我会崩溃,指示的行是:timer=controller.timer
在TimerViewController文件中,将行var timer:TimerModel=TimerModel()更改为var timer:TimerModel,也在我的回答中编辑好对不起,但是这样我就崩溃了,请看屏幕:嗨,如果可能的话,你能发送整个项目吗。那么调试就很容易了……嗨,这里有一个类似练习的链接:我不知道当我单击DetailViewCotroller标签中的按钮时,如何设置代码以拥有一个新的TableView。一点一点我就崩溃了。