需求:利用代理实现反响传值(以下例子采用点击第二个视图控制器中的按钮来改变第一个视图控制器中的Label的内容)
一、创建RootViewController
- importFoundation
- importUIKit
- classRootViewController:UIViewController,ChangeWordDelegate{
- varqzLabel:UILabel?
- overridefuncviewDidLoad(){
- super.viewDidLoad()
- self.title="RootViewController"
- letrightBtn:UIBarButtonItem=UIBarButtonItem(title:"下一页",style:.Plain,target:self,action:"nextPage")
- self.navigationItem.rightBarButtonItem=rightBtn
- letrect=CGRect(x:0,y:200,width:320,height:50)
- qzLabel=UILabel(frame:rect)
- qzLabel!.text="秦志伟"
- qzLabel!.textAlignment=NSTextAlignment.Center
- self.view.addSubview(qzLabel)
- }
- funcnextPage(){
- letsvc=SecondViewController()
- svc.delegate=self
- self.navigationController.pushViewController(svc,animated:true)
- }
- funcchangeWord(controller:SecondViewController,string:String){
- qzLabel!.text=string
- println("qzLabel.text==\(string)")
- }
二、创建SecondViewController