简体   繁体   English

如何将值从一个视图传递到 iOS 中的另一个视图?

[英]How can I pass the value from a view to another view in iOS?

    import UIKit

    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)
        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();               
                        }
                    }
                }
            }
        }
    }
}

I've this code, and works well on table controller view, but I want to make a new view controller that prints json ( json on table controller works well, and printed out)我有这段代码,并且在表控制器视图上运行良好,但我想制作一个打印json的新视图控制器(表控制器上的json运行良好,并打印出来)

So, I made a new view controller connected to another class named senddata what command that I should use to print json on there, I want to print that json data when the button pressed:因此,我创建了一个连接到另一个名为senddata类的新视图控制器,我应该使用什么命令在那里打印json ,我想在按下按钮时打印该json数据:

import UIKit

class SendData: UIViewController{

    @IBAction func tes(sender: AnyObject) {
        println()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }    
}

You could put that JSON Data in a global struct, so it would end up looking like this.您可以将该 JSON 数据放在全局结构中,因此它最终看起来像这样。

TableViewController.swift 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发送数据.swift

class SendData: UIViewController{

    @IBAction func tes(sender: AnyObject) {
        println(JSON.JSONData)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在IOS中将数据从一个视图传递到另一个视图控制器? - How to pass data from one view to another view controller in IOS? 如何将手势识别器从一个视图传递到另一个视图 - How can I pass a gesture recognizer from one view to another 如何将值从一个视图传递到另一视图 - How to pass a value from one view to another view 如何访问值表单completionHandler并将其传递给另一个视图IOS - how to access value form completionHandler and pass it to another view IOS iOS在NSMutableArray中保留一个值,并将其传递给另一个视图 - iOS retain a value in a NSMutableArray and pass the to another view 如何从Firebase检索数据到表视图控制器并将其传递给另一个视图控制器 - how can I retrieve data from firebase to a table view controller and pass it to another view controller 如何将IBOutlet值从视图传递到VIPER iO中的交互器? - How to pass IBOutlet value from view to interactor in VIPER iOs? 如何将值从一个视图控制器传递给另一个swift? - How to pass value from one view controller to another swift? 从JSON获取价值并传递到另一个视图 - Get value from JSON and pass to another view 我如何在一个视图中退出UILongPressGesture并将其传递给另一个视图? - how can i resign the UILongPressGesture in one view and pass it to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM