如何将值从视图传递到iOS中的另一个视图?
import UIKit class TableController: UITableViewController { var items = NSMutableArray() var TableData:Array = Array () var json:String = "" var quantity2: Int = 0 var shortDate: String = "" @IBOutlet var myTableView: UITableView! var arrayOfMenu: [Nutrisi] = [Nutrisi]() override func viewDidLoad() { super.viewDidLoad() self.setUpMenu() self.myTableView.delegate = self self.myTableView.dataSource = self } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arrayOfMenu.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("cell") as! CustomCell let menu = arrayOfMenu[indexPath.row] cell.setCell(menu.type, rightlabeltext: menu.unit, imagename: menu.image) var data = Nutritiondata(type: menu.type, amount: String(cell.value).toInt()!) var json = JSONSerializer.toJson(data) self.json = json return cell } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func do_table_refresh() { dispatch_async(dispatch_get_main_queue(), { self.tableView.reloadData() return }) } func setUpMenu() { var json: JSON = JSON (data: NSData()) let frame:CGRect = CGRect(x: 110, y: 300, width: view.frame.width, height: 700) self.tableView.frame = frame DataManager.getnutritionsDataFromFileWithSuccess{ (data) -> Void in json = JSON(data: data) let results = json["results"] for (index: String, subJson: JSON) in results { } for (var i = 0; i < json["nutritions"].count; i++) { if let type = json["nutritions"][i]["type"].string { if let icon: AnyObject = json["nutritions"][i]["icon"].string { self.items.addObject(icon) if let unit = json["nutritions"][i]["unit"].string { dispatch_async(dispatch_get_main_queue(), {self.tableView!.reloadData()}) var menu = Nutrisi(type: type, unit: unit, image: icon as! String) self.arrayOfMenu.append(menu) self.TableData.append(type + unit ) self.do_table_refresh(); } } } } } } }
我有这个代码,并且在表控制器视图上运行良好,但是我想创建一个新的视图控制器来打印json
(表控制器上的json
运行良好,并打印出来)
所以,我创建了一个新的视图控制器连接到另一个名为senddata
类,我应该用什么命令在那里打印json
,我想在按下按钮时打印出json
数据:
import UIKit class SendData: UIViewController{ @IBAction func tes(sender: AnyObject) { println() } override func viewDidLoad() { super.viewDidLoad() } }
你可以将JSON数据放在一个全局结构中,所以最终会看起来像这样。
TableViewController.swift
struct JSON { static var JSONData = "" } class TableController: UITableViewController { var items = NSMutableArray() var TableData:Array< String > = Array < String >() var json:String = "" var quantity2: Int = 0 var shortDate: String = "" @IBOutlet var myTableView: UITableView! var arrayOfMenu: [Nutrisi] = [Nutrisi]() override func viewDidLoad() { super.viewDidLoad() self.setUpMenu() self.myTableView.delegate = self self.myTableView.dataSource = self } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arrayOfMenu.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("cell") as! CustomCell let menu = arrayOfMenu[indexPath.row] cell.setCell(menu.type, rightlabeltext: menu.unit, imagename: menu.image) var data = Nutritiondata(type: menu.type, amount: String(cell.value).toInt()!) var json = JSONSerializer.toJson(data) JSON.JSONData = json return cell }
SendData.swift
class SendData: UIViewController{ @IBAction func tes(sender: AnyObject) { println(JSON.JSONData) } override func viewDidLoad() { super.viewDidLoad() } }