Swift-Pure Code Implementation page Segue jump, and parameter passing

Source: Internet
Author: User
Tags uikit
The following illustrates an example of how to switch segue pages and pass parameters in the code.


The sample functions are as follows:
1. The main interface is a list (this list is implemented in the code)
2. When you click a list item, the interface will switch to the detail page, and at the same time transfer the value of the changed list item to the detail page.

The effect chart is as follows:
Technology sharing Technology sharing

Implementation steps:
1. Drag a new ViewController into the storyboard as a detail page, and create a new class DetailViewController that inherits ViewController. And it is bound to the view and the controller with the newly created detail page in the storyboard.
technology sharing
2. In the storyboard, select the detail page and drag and drop the Detail View Controller on the top to the main page for segue association (show detail)
technology sharing
3. Select the associated line and set segue's Identifier property to "ShowDetailView"
technology sharing
4. The main interface code ViewController.swift

import UIKit
 
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
     
    var ctrlnames: [String] = ["Task 1", "Task 2", "Task 3"]
    var tableView: UITableView?
     
    override func loadView () {
        super.loadView ()
    }
     
    override func viewDidLoad () {
        super.viewDidLoad ()
        // Create table view
        self.tableView = UITableView (frame: self.view.frame, style: UITableViewStyle.Plain)
        self.tableView! .delegate = self
        self.tableView! .dataSource = self
        // Create a reused cell
        self.tableView! .registerClass (UITableViewCell.self, forCellReuseIdentifier: "SwiftCell")
        self.view.addSubview (self.tableView!)
    }
     
    // In this example, there is only one partition
    func numberOfSectionsInTableView (tableView: UITableView!)-> Int {
        return 1;
    }
     
    // Return the number of table rows (that is, return the number of controls)
    func tableView (tableView: UITableView, numberOfRowsInSection section: Int)-> Int {
        return self.ctrlnames.count
    }
     
    // Create the display content of each unit (create the unit specified by the parameter indexPath)
    func tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
        -> UITableViewCell
    {
        // In order to provide table display performance, the created unit needs to be reused
        let identify: String = "SwiftCell"
        // Cells of the same form are reused and registered at the time of declaration
        let cell = tableView.dequeueReusableCellWithIdentifier (identify, forIndexPath: indexPath)
            as UITableViewCell
        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
        cell.textLabel? .text = self.ctrlnames [indexPath.row]
        return cell
    }
     
    // UITableViewDelegate method to handle the selected event of the list item
    func tableView (tableView: UITableView !, didSelectRowAtIndexPath indexPath: NSIndexPath!)
    {
        self.tableView! .deselectRowAtIndexPath (indexPath, animated: true)
        var itemString = self.ctrlnames [indexPath.row]
      
        self.performSegueWithIdentifier ("ShowDetailView", sender: itemString)
    }
     
    // In this method, pass parameters to the new page
    override func prepareForSegue (segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "ShowDetailView" {
            let controller = segue.destinationViewController as DetailViewController
            controller.itemString = sender as? String
        }
    }
     
    override func didReceiveMemoryWarning () {
        super.didReceiveMemoryWarning ()
    }
}

5. Detail page code DetailViewController.swift


import UIKit
 
class DetailViewController: UIViewController {
     
    var itemString: String?
 
    @IBOutlet weak var textField: UITextField!
     
    override func viewDidLoad () {
        super.viewDidLoad ()
 
        textField.text = itemString
    }
 
    override func didReceiveMemoryWarning () {
        super.didReceiveMemoryWarning ()
    }
}
Swift-Pure code to achieve page segue jump and parameter passing

label:

Original address: http://www.cnblogs.com/Free-Thinker/p/4841064.html
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.