微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift - 纯代码实现页面segue跳转,以及参数传递

下面通过一个例子说明如何在代码中进行segue页面的切换,以及参数的传递。

样例功能如下:
1,主界面中是一个列表(这个列表是在代码中实现)
2,点击列表项时,界面会切换到详情页面,同时传递改列表项的值到详细页面
效果图如下:

实现步骤:
1,在storyboard中拖入一个新的ViewController用做详情页面,同时创建一个继承ViewController的新类DetailViewController。并将其与storyboard中新建的详情页面进行视图与控制器的绑定。

2,在storyboard中,选中详情页面,通过最上方的Detail View Controller拖拽到主页面进行segue关联(show detail)
(右键点击 Detail View Controller 头部黄色的标志,在出现的菜单中选择“show detail”旁边的圆圈,在圆圈上按住左键拖动到主页面
关联后如下:
3,选中关联线,设置segue的Identifier属性为“ShowDetailView”

4,主界面代码 ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import UIKit
class ViewController : UIViewController , UITableViewDelegate UITableViewDataSource {
var ctrlnames:[ String ] = [ "任务1" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas, "任务2" "任务3" ]
tableView: UITableView ?
override func loadView() {
super .loadView()
}
viewDidLoad() {
.viewDidLoad()
//创建表视图
self .tableView = (frame: .view.frame,style: UITableViewStyle . Plain )
.tableView!.delegate = self
.tableView!.dataSource = self
//创建一个重用的单元格
.tableView!.registerClass( UITableViewCell . ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,forCellReuseIdentifier: "cell1" )
.view.addSubview( .tableView!)
}
//在本例中,只有一个分区
numberOfSectionsInTableView(tableView: ) -> Int {
return 1;
}
//返回表格行数(也就是返回控件数)
tableView(tableView: ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,numberOfRowsInSection section: {
return .ctrlnames.count
}
//创建各单元显示内容(创建参数indexPath指定的单元)
NSIndexPath )
-> UITableViewCell
{
//为了提供表格显示性能,已创建完成的单元需重复使用
let identify: = "cell1"
//同一形式的单元格重复使用,在声明时已注册
cell = tableView.dequeueReusableCellWithIdentifier(identify,
forIndexPath: indexPath) as UITableViewCell
cell.accessoryType = UITableViewCellAccessoryType disclosureIndicator
cell.textLabel?.text = .ctrlnames[indexPath.row]
cell
}
// UITableViewDelegate 方法,处理列表项的选中事件
)
{
.tableView!.deselectRowAtIndexPath(indexPath,animated: true )
itemString = .ctrlnames[indexPath.row]
.performSegueWithIdentifier( "ShowDetailView" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,sender: itemString)
}
//在这方法中给新页面传递参数
prepareForSegue(segue: UIStoryboardSegue ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,sender: AnyObject ?) {
if segue.identifier == {
controller = segue.destinationViewController as ! DetailViewController
controller.itemString = sender ? String
}
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
}

5,详情页面代码 DetailViewController.swift
18
DetailViewController itemString: ?
@IBOutlet weak textField: UITextField !
viewDidLoad() {
.viewDidLoad()
textField.text = itemString
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
原文出自: www.hangge.com 转载请保留原文链接 http://www.hangge.com/blog/cache/detail_720.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐