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?
// 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]
// 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
}
}
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.