我有一个名为ThirdViewController的表视图控制器,它显示了一个事务列表。当用户按下添加按钮时,应用程序会切换到FifthViewController,在那里他们可以输入交易名称,值和日期。我试图将在FifthViewController中输入的数据放入一个名为ArrayData的类中,以便我可以将它添加到数组中,然后将付款添加到ThirdViewController表视图中。
这是我的ThirdViewController.swift的代码:
var arrayObject = ArrayData()
class ThirdViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// #pragma mark - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return arrayObject.paymentsArray().count
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell:CustomTransactionTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomTransactionTableViewCell
cell.paymentNameLabel.text = (arrayObject.paymentsArray().objectAtIndex(indexPath.row)) as String
cell.costLabel.text = (arrayObject.costArray().objectAtIndex(indexPath.row)) as String
cell.dateLabel.text = (arrayObject.dateArray().objectAtIndex(indexPath.row)) as String
return cell
}
}
这是arrayData.swift的代码:
class ArrayData: NSObject {
func paymentsArray() -> NSMutableArray {
var arrayDataPayments: NSMutableArray = ["Test"]
return arrayDataPayments
}
func costArray() -> NSMutableArray {
var arrayDataCost: NSMutableArray = ["£100"]
return arrayDataCost
}
func dateArray() -> NSMutableArray {
var arrayDataDate: NSMutableArray = ["12/07/14"]
return arrayDataDate
}
}
这是FifthViewController的代码:
class FifthViewController: UIViewController {
@IBOutlet var transactionNameInput : UITextField
@IBOutlet var transactionDateInput : UITextField
@IBOutlet var transactionValueInput : UITextField
@IBOutlet var addBackButton : UIButton
@IBAction func addTransactionButton(sender : AnyObject) {
self.navigationController.popToRootViewControllerAnimated(true)
var transactionName = transactionNameInput.text
var transactionDate = transactionDateInput.text
var transactionValue = transactionValueInput.text
}
@IBAction func viewTapped(sender : AnyObject) {
//Closes keyboard when user touches screen
transactionDateInput.resignFirstResponder()
transactionNameInput.resignFirstResponder()
transactionValueInput.resignFirstResponder()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
如何从FifthViewController获取变量到ArrayData类,以便将它们添加到数组中?这可能非常简单,但我是Xcode的新手,显然是Swift。
非常感谢任何帮助。
答案 0 :(得分:1)
每个调用返回一个新数组时,ArrayData
类中的方法实现笨拙。也许这就是你真正想要的,但这很不寻常(而且你已经注意到你是Xcode和Swift的新手,因此我怀疑它并不是你想要的)。更多正常实现将是:
class ArrayData: NSObject {
var arrayDataPayments: NSMutableArray = ["Test"]
var arrayDataCost: NSMutableArray = ["£100"]
var arrayDataDate: NSMutableArray = ["12/07/14"]
func paymentsArray() -> NSMutableArray { return arrayDataPayments }
func costArray() -> NSMutableArray { return arrayDataCost }
func dateArray() -> NSMutableArray { return arrayDataDate }
}
现在,我并不是说这将解决您在控制器中获取数据的更大问题;但是,我怀疑以上是一个更好的开始。