(本文代碼已升級(jí)至Swift3)
下面通過一個(gè)例子說明如何在代碼中進(jìn)行segue頁面的切換订咸,以及參數(shù)的傳遞曼尊。
樣例功能如下:
1,主界面中是一個(gè)列表(這個(gè)列表是在代碼中實(shí)現(xiàn))
2脏嚷,點(diǎn)擊列表項(xiàng)時(shí)骆撇,界面會(huì)切換到詳情頁面,同時(shí)傳遞改列表項(xiàng)的值到詳細(xì)頁面父叙。
效果圖如下:
原文:Swift - 純代碼實(shí)現(xiàn)頁面segue跳轉(zhuǎn)神郊,以及參數(shù)傳遞 原文:Swift - 純代碼實(shí)現(xiàn)頁面segue跳轉(zhuǎn),以及參數(shù)傳遞
實(shí)現(xiàn)步驟:
1趾唱,在storyboard中拖入一個(gè)新的 ViewController 用做詳情頁面屿岂,同時(shí)創(chuàng)建一個(gè)繼承ViewController的新類 DetailViewController。并將其與storyboard中新建的詳情頁面進(jìn)行視圖與控制器的綁定鲸匿。
原文:Swift - 純代碼實(shí)現(xiàn)頁面segue跳轉(zhuǎn),以及參數(shù)傳遞
2阻肩,在storyboard中带欢,選中詳情頁面,通過最上方的Detail View Controller拖拽到主頁面進(jìn)行segue關(guān)聯(lián)(show detail)
(右鍵點(diǎn)擊 Detail View Controller 頭部黃色的標(biāo)志烤惊,在出現(xiàn)的菜單中選擇“show detail”旁邊的圓圈乔煞,在圓圈上按住左鍵拖動(dòng)到主頁面)
原文:Swift - 純代碼實(shí)現(xiàn)頁面segue跳轉(zhuǎn),以及參數(shù)傳遞
關(guān)聯(lián)后如下:
原文:Swift - 純代碼實(shí)現(xiàn)頁面segue跳轉(zhuǎn)柒室,以及參數(shù)傳遞
3渡贾,選中關(guān)聯(lián)線,設(shè)置segue的 Identifier 屬性為“ShowDetailView”
原文:Swift - 純代碼實(shí)現(xiàn)頁面segue跳轉(zhuǎn)雄右,以及參數(shù)傳遞
4空骚,主界面代碼 ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var ctrlnames:[String] = ["任務(wù)1","任務(wù)2","任務(wù)3"]
var tableView:UITableView?
override func loadView() {
super.loadView()
}
override func viewDidLoad() {
super.viewDidLoad()
//創(chuàng)建表視圖
self.tableView = UITableView(frame: self.view.frame, style:.plain)
self.tableView!.delegate = self
self.tableView!.dataSource = self
//創(chuàng)建一個(gè)重用的單元格
self.tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "cell1")
self.view.addSubview(self.tableView!)
}
//在本例中,只有一個(gè)分區(qū)
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
//返回表格行數(shù)(也就是返回控件數(shù))
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.ctrlnames.count
}
//創(chuàng)建各單元顯示內(nèi)容(創(chuàng)建參數(shù)indexPath指定的單元)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
-> UITableViewCell {
//為了提供表格顯示性能擂仍,已創(chuàng)建完成的單元需重復(fù)使用
let identify:String = "cell1"
//同一形式的單元格重復(fù)使用囤屹,在聲明時(shí)已注冊(cè)
let cell = tableView.dequeueReusableCell(withIdentifier: identify,
for: indexPath) as UITableViewCell
cell.accessoryType = .disclosureIndicator
cell.textLabel?.text = self.ctrlnames[indexPath.row]
return cell
}
// UITableViewDelegate 方法,處理列表項(xiàng)的選中事件
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView!.deselectRow(at: indexPath, animated: true)
let itemString = self.ctrlnames[indexPath.row]
self.performSegue(withIdentifier: "ShowDetailView", sender: itemString)
}
//在這個(gè)方法中給新頁面?zhèn)鬟f參數(shù)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ShowDetailView"{
let controller = segue.destination as! DetailViewController
controller.itemString = sender as? String
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
5逢渔,詳情頁面代碼 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()
}
}
注:如果想在詳細(xì)頁中返回主頁面肋坚,可以在詳細(xì)頁中添加一個(gè)返回按鈕,按鈕響應(yīng)代碼如下:
//返回到上一個(gè)頁面
self.presentingViewController!.dismiss(animated: true, completion: nil)
原文出自:www.hangge.com 轉(zhuǎn)載請(qǐng)保留原文鏈接:http://www.hangge.com/blog/cache/detail_720.html