{"id":1606,"date":"2021-03-07T12:39:54","date_gmt":"2021-03-07T12:39:54","guid":{"rendered":"https:\/\/sezeromer.com\/?p=1606"},"modified":"2023-03-04T13:32:00","modified_gmt":"2023-03-04T10:32:00","slug":"swift-sayfalar-arasi-veri-gonderme","status":"publish","type":"post","link":"https:\/\/sezeromer.com\/en\/swift-sayfalar-arasi-veri-gonderme\/","title":{"rendered":"Swift Data Transfer Between Pages"},"content":{"rendered":"<p>Hello friends, in this article, we will talk about how to send data between two pages with <strong>Swift<\/strong>. In the Het application, there is actually a communication between the pages. For example, if you have a list on the home page and we click on an element of this list, we should show the details of it.<\/p>\n<h2>Sample<\/h2>\n<p>In the example we will do, we will have a fruit model and we will talk about how many calories there are in detail. Let&#8217;s show the names of the fruits in our fruit list on our first page. When the list is clicked, let&#8217;s go into the detail and write how many calories there are in capital letters. Our design screen looks like this.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2018\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/03\/Screen-Shot-2021-03-07-at-15.29.30-1024x556.png\" alt=\"\" width=\"640\" height=\"348\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/03\/Screen-Shot-2021-03-07-at-15.29.30-1024x556.png 1024w, https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/03\/Screen-Shot-2021-03-07-at-15.29.30-300x163.png 300w, https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/03\/Screen-Shot-2021-03-07-at-15.29.30-768x417.png 768w, https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/03\/Screen-Shot-2021-03-07-at-15.29.30-500x271.png 500w, https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/03\/Screen-Shot-2021-03-07-at-15.29.30-800x434.png 800w, https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/03\/Screen-Shot-2021-03-07-at-15.29.30-1280x695.png 1280w, https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/03\/Screen-Shot-2021-03-07-at-15.29.30-1920x1042.png 1920w, https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/03\/Screen-Shot-2021-03-07-at-15.29.30-1536x834.png 1536w, https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/03\/Screen-Shot-2021-03-07-at-15.29.30-2048x1112.png 2048w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2>Model<\/h2>\n<p>We need to create one fruit model. For this, it will be sufficient to create a model that keeps the name of the fruit and how many calories.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">struct FruitModel {\r\n    let name: String\r\n    let calorie: Int\r\n}<\/pre>\n<\/div>\n<h2>Controller<\/h2>\n<p>Now we keep a set of sample fruits and their calories in our Controller for listing. Then we connect it to our UITableView. We need to keep a variable for the selected fruit. We need to replace this variable with the fruit that the user chooses each time he chooses a fruit. After changing it, we need to run Segue, which allows you to go to our detail page from our list page. We gave this <strong>Segue<\/strong> the &#8220;showDetail&#8221; Identifier. After running Segue, we assign the variable we selected to the fruit variable on the detail page with the prepareSegue function.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">\/\/\r\n\/\/  ViewController.swift\r\n\/\/  SwiftDataTransfer\r\n\/\/\r\n\/\/  Created by Omer Sezer on 7.03.2021.\r\n\/\/\r\n\r\nimport UIKit\r\n\r\nclass ViewController: UIViewController {\r\n\r\n    @IBOutlet weak var tvFruits: UITableView!\r\n    \r\n    var selectedFruit: FruitModel?\r\n    \r\n    let models: [FruitModel] = [\r\n        FruitModel(name: \"Apple\", calorie: 95),\r\n        FruitModel(name: \"Avocado\", calorie: 320),\r\n        FruitModel(name: \"Banana\", calorie: 111),\r\n        FruitModel(name: \"Cherries\", calorie: 4),\r\n        FruitModel(name: \"Kiwi\", calorie: 112),\r\n        FruitModel(name: \"Lime\", calorie: 20),\r\n        FruitModel(name: \"Mango\", calorie: 220),\r\n        FruitModel(name: \"Strawberries\", calorie: 49),\r\n        FruitModel(name: \"Peach\", calorie: 59),\r\n        FruitModel(name: \"Orange\", calorie: 62)\r\n    ]\r\n    \r\n    override func viewDidLoad() {\r\n        super.viewDidLoad()\r\n        setUI()\r\n    }\r\n    \r\n    func setUI() {\r\n        \/\/ MARK: title\r\n        title = \"Fruits\"\r\n        \r\n        \/\/ MARK: tvFruits\r\n        tvFruits.delegate = self\r\n        tvFruits.dataSource = self\r\n    }\r\n    \r\n    func goDetail(fruit: FruitModel) {\r\n        selectedFruit = fruit\r\n        performSegue(withIdentifier: \"showDetail\", sender: nil)\r\n    }\r\n    \r\n    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {\r\n        if segue.identifier == \"showDetail\" {\r\n            let detailVC = segue.destination as! DetailViewController\r\n            detailVC.fruit = selectedFruit\r\n        }\r\n    }\r\n}\r\n\r\nextension ViewController: UITableViewDelegate {\r\n    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {\r\n        tableView.deselectRow(at: indexPath, animated: true)\r\n        goDetail(fruit: models[indexPath.row])\r\n    }\r\n    \r\n}\r\n\r\nextension ViewController: UITableViewDataSource {\r\n    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {\r\n        return models.count\r\n    }\r\n    \r\n    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {\r\n        let cell = UITableViewCell()\r\n        cell.textLabel?.text = models[indexPath.row].name\r\n        return cell\r\n    }\r\n}<\/pre>\n<\/div>\n<p>After assigning the selected fruit to the detail page, it remains only to show it on the screen. We can do this inside <strong>viewDidLoad<\/strong>.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">\/\/\r\n\/\/  DetailViewController.swift\r\n\/\/  SwiftDataTransfer\r\n\/\/\r\n\/\/  Created by Omer Sezer on 7.03.2021.\r\n\/\/\r\n\r\nimport UIKit\r\n\r\nclass DetailViewController: UIViewController {\r\n\r\n    @IBOutlet weak var lblFruitName: UILabel!\r\n    @IBOutlet weak var lblFruitCalorie: UILabel!\r\n    \r\n    var fruit: FruitModel?\r\n    \r\n    override func viewDidLoad() {\r\n        super.viewDidLoad()\r\n\r\n        setUI()\r\n    }\r\n    \r\n    func setUI() {\r\n        lblFruitName.text = fruit?.name\r\n        lblFruitCalorie.text = \"\\(fruit?.calorie ?? 0) cal\"\r\n    }\r\n\r\n}<\/pre>\n<\/div>\n<p>Our screen output is as follows.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2019\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/03\/1.gif\" alt=\"\" width=\"334\" height=\"721\" \/><\/p>\n<p>If you have questions, you can reach by e-mail or comment. Good work.<\/p>","protected":false},"excerpt":{"rendered":"<p>Hello friends, in this article, we will talk about how to send data between two pages with Swift. In the Het application, there is actually a communication between the pages. For example, if you have a list on the home page and we click on an element of this list, we should show the details [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2019,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[327],"tags":[496,782,206,781,780,287,328,724,778,497,779],"class_list":["post-1606","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift","tag-data","tag-gonderme","tag-mvc","tag-prepare-segue","tag-segue","tag-storyboard","tag-swift","tag-tableview","tag-transfer","tag-veri","tag-view-contrller"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/1606"}],"collection":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/comments?post=1606"}],"version-history":[{"count":5,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/1606\/revisions"}],"predecessor-version":[{"id":2666,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/1606\/revisions\/2666"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media\/2019"}],"wp:attachment":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media?parent=1606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/categories?post=1606"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/tags?post=1606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}