页面传值2

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #008400}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; min-height: 15.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px 'PingFang SC'; color: #008400}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #4f8187}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #703daa}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #3d1d81}p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #008400; min-height: 15.0px}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font: 13.0px 'PingFang SC'; font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font: 13.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s5 {font: 13.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s6 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s7 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s8 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s9 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s10 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s11 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s12 {font: 13.0px 'PingFang SC'; font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s13 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s14 {font: 13.0px 'PingFang SC'; font-variant-ligatures: no-common-ligatures; color: #008400}span.s15 {font-variant-ligatures: no-common-ligatures; color: #31595d}

//
// SecondViewController.swift
// 页面传值
//
// Created by SZT on 2016/11/4.
// Copyright © 2016年 SZT. All rights reserved.
//

import UIKit

//从后往前传值的协议
protocol secondProtocol{
//需要传什么类型的参数,参数列表就写什么
func translateString(str:String)

}

class SecondViewController: UIViewController {
//定义labelStr属性,负责接收前一个页面传过来的值
var labelStr:String?
var textField:UITextField?

//定义代理属性
var delegate:secondProtocol?

override func viewDidLoad() {
    super.viewDidLoad()
    //创建文本框textField
    textField = UITextField(frame: CGRectMake(50, 150, 90, 40))
    textField?.borderStyle = .RoundedRect
    textField?.placeholder = "请输入内容"
    //将传过来的值显示出来
    textField?.text = labelStr
    view.addSubview(textField!)
    view.backgroundColor = UIColor.whiteColor()
    navigationItem.title = "SecondVC"
    let btn = UIButton(frame: CGRectMake(50,200,90,40))
    btn.backgroundColor = UIColor.grayColor()
    btn.setTitle("pop", forState: .Normal)
    btn.addTarget(self, action: "popToFirstVC", forControlEvents: .TouchUpInside)
    view.addSubview(btn)
    
    let modelBtn = UIButton(frame: CGRectMake(50,260,90,40))
    modelBtn.backgroundColor = UIColor.cyanColor()
    modelBtn.setTitle("模态显示", forState: .Normal)
    modelBtn.addTarget(self, action: "modelToThird", forControlEvents: .TouchUpInside)
    view.addSubview(modelBtn)
}

func modelToThird(){
    let thirdVC = ThirdViewController()
    presentViewController(thirdVC, animated: true, completion: nil)
}

func popToFirstVC(){
    //代理传值
    delegate?.translateString((textField?.text)!)
    navigationController?.popViewControllerAnimated(true)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
 // MARK: - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
 // Get the new view controller using segue.destinationViewController.
 // Pass the selected object to the new view controller.
 }
 */

}

你可能感兴趣的:(页面传值2)