// AppDelegate.swift
// ReverseSendValue
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let first=FirstViewController()
first.view.backgroundColor=UIColor.whiteColor()
let navi=UINavigationController(rootViewController: first)
window?.rootViewController=navi
return true
}
}
// FirstViewController.swift
// ReverseSendValue
import UIKit
//屏幕尺寸
let kWidth=UIScreen.mainScreen().bounds.size.width
let kHeight=UIScreen.mainScreen().bounds.size.height
//遵守SecondViewControllerDelegate协议
class FirstViewController: UIViewController,SecondViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
//搭建视图
createUI()
}
//搭建视图
func createUI(){
let frame=CGRectMake(UIScreen.mainScreen().bounds.size.width/2-120, 200, 240, 50)
let resultLb=UILabel(frame: frame)
resultLb.text="[email protected]"
resultLb.textAlignment=NSTextAlignment.Center
self.view.addSubview(resultLb)
resultLb.tag=100
let btn=UIButton()
btn.frame=CGRectMake(kWidth/2-40, CGRectGetMaxY(resultLb.frame)+20, 100, 40)
btn.setTitle("修 改", forState: .Normal)
btn.backgroundColor=UIColor.blueColor()
btn.addTarget(self, action: Selector("gotoChangeValue"), forControlEvents: .TouchUpInside)
self.view.addSubview(btn)
}
//跳转到修改的视图
func gotoChangeValue(){
let second=SecondViewController()
let lb=self.view.viewWithTag(100) as! UILabel
second.text=lb.text
//设置代理
second.delegate=self
self.navigationController?.pushViewController(second, animated: true)
}
//代理方法
func sendValue(value:String?){
let lb=self.view.viewWithTag(100) as! UILabel
lb.text=value
}
}
// SecondViewController.swift
// ReverseSendValue
import UIKit
//协议
protocol SecondViewControllerDelegate{
//协议方法
func sendValue(value:String?)
}
class SecondViewController: UIViewController {
var text:String?
//代理属性
var delegate:SecondViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor=UIColor.whiteColor()
//搭建界面
createUI()
}
//搭建界面
func createUI(){
let textField=UITextField()
textField.frame=CGRectMake(kWidth/2-120, 200, 240, 50)
textField.text=text
textField.borderStyle=UITextBorderStyle.RoundedRect
textField.keyboardType=UIKeyboardType.EmailAddress
textField.textAlignment=NSTextAlignment.Center
self.view.addSubview(textField)
textField.tag=101
let btn=UIButton()
btn.frame=CGRectMake(UIScreen.mainScreen().bounds.size.width/2-40, CGRectGetMaxY(textField.frame)+20, 100, 40)
btn.setTitle("修改完成", forState: .Normal)
btn.backgroundColor=UIColor.orangeColor()
btn.addTarget(self, action: Selector("doneAction"), forControlEvents: .TouchUpInside)
self.view.addSubview(btn)
}
//完成修改
func doneAction(){
let tf=self.view.viewWithTag(101) as! UITextField
if((delegate) != nil)
{
delegate?.sendValue(tf.text)
self.navigationController?.popViewControllerAnimated(true)
}
}
//隐藏键盘
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
}