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

Source: Internet
Author: User
Tags uikit

Here's an example of how to switch the Segue page in code and pass the parameters.

The sample functions are as follows: 1, the main interface is a list (this list is implemented in code) 2, when you click on the list item, the interface will switch to the detail page, while passing the value of the list item to the detailed page. Follow these steps: 1, drag a new viewcontroller into the storyboard as a detail page, and create a new class Detailviewcontroller that inherits Viewcontroller. The view is bound to the controller with the new detail page in storyboard. 2, in storyboard, select the Details page, drag the top detail View controller to the main page for Segue Association (show detail) 3, select the correlation line, set Segue identifier property to " Showdetailview "
4, 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, 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 indexPath)
    func tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
        -> UITableViewCell
        // In order to provide table display performance, the created units need to be reused
        let identify: String = "SwiftCell"
        // The 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, 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)
    // Pass parameters to the new page in this method
    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() {
        textField.text = itemString
    override func didReceiveMemoryWarning() {

Original from: www.hangge.com reprint please keep the original link: http://www.hangge.com/blog/cache/detail_720.html

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

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.