使用协议Protocol实现ViewController之间传值
2015-12-17 13:50
239 查看
1、定义协议Protocol 名为PassValue
//定义一个协议
protocol PassValue {
func passValue(value:String)
}
2、定义两个ViewController , 名为 ViewController1 和 ViewController2 。
3、实现ViewController1到ViewController2正向传值,并在本页实现协议设置delegate
class ViewController1:
UIViewController,PassValue{
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title =
"ViewController"
self.navigationItem.rightBarButtonItem =
UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target:
self, action:
"pushVc")
}
func pushVc(){
var vc2 = ViewController2()
vc2.name =
"Vc1 Value"
vc2.delegate =
self //设置代理方法
self.navigationController?.pushViewController(vc2, animated:
true)
}
//委托的方法
func passValue(value:String){
//将返回的数据显示在label控件上
var label2 = UILabel(frame:
CGRect(x: 10, y:
80, width: 120, height:
40))
label2.text = value
self.view.addSubview(label2)
}
}
4、实现ViewController2到ViewController1反向传值
class ViewController2:
UIViewController {
var name = ""
var delegate:PassValue?
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title =
"ViewController2"
self.navigationItem.leftBarButtonItem =
UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Refresh, target:
self, action:
"popVc")
var label1 = UILabel(frame:
CGRect(x: 10, y:
80, width: 130, height:
40))
label1.text =
name
self.view.addSubview(label1)
}
func popVc(){
delegate?.passValue(" My Return ") //协议中的方法
self.navigationController?.popViewControllerAnimated(true)
}
}
//定义一个协议
protocol PassValue {
func passValue(value:String)
}
2、定义两个ViewController , 名为 ViewController1 和 ViewController2 。
3、实现ViewController1到ViewController2正向传值,并在本页实现协议设置delegate
class ViewController1:
UIViewController,PassValue{
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title =
"ViewController"
self.navigationItem.rightBarButtonItem =
UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target:
self, action:
"pushVc")
}
func pushVc(){
var vc2 = ViewController2()
vc2.name =
"Vc1 Value"
vc2.delegate =
self //设置代理方法
self.navigationController?.pushViewController(vc2, animated:
true)
}
//委托的方法
func passValue(value:String){
//将返回的数据显示在label控件上
var label2 = UILabel(frame:
CGRect(x: 10, y:
80, width: 120, height:
40))
label2.text = value
self.view.addSubview(label2)
}
}
4、实现ViewController2到ViewController1反向传值
class ViewController2:
UIViewController {
var name = ""
var delegate:PassValue?
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title =
"ViewController2"
self.navigationItem.leftBarButtonItem =
UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Refresh, target:
self, action:
"popVc")
var label1 = UILabel(frame:
CGRect(x: 10, y:
80, width: 130, height:
40))
label1.text =
name
self.view.addSubview(label1)
}
func popVc(){
delegate?.passValue(" My Return ") //协议中的方法
self.navigationController?.popViewControllerAnimated(true)
}
}
相关文章推荐
- Android 上传图片到服务器
- ng-view和ng-include之间的区别
- QT封装qlabel可以实现画图,画图拖动
- Unable to lock the administration directory (/var/lib/dpkg/),is another process using it?
- CSS学习中遇到的问题记录
- iPhone5s的App Store不能下载应用的解决方案
- 页面定时跳转(倒计时跳转)代码总结
- CXF Web Service入门实例一
- iOS开发数据库篇—FMDB简单介绍
- QT下显示中文
- 83. 从视图索引说Notes数据库(上)
- C#中判断字符串相等的方法
- BZOJ3237: [Ahoi2013]连通图
- oracle 主键 唯一约束 索引
- 【JS第一本】head first Javascript 读书笔记
- hadoop安装
- 【待解决】maven创建web报Cannot read lifecycle mapping metadata for artifact org.apache.maven.plugins
- 这条路,是不是应该走下去
- 如何给数百万考生的成绩排序
- 【Java】Java中文分词器Ansj的使用