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() {
super.viewDidLoad()
textField.text = itemString
}
override func didReceiveMemoryWarning() {
super.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