//一、AppDelegate.swift
//
//AppDelegate.swift
//页面传值
//
//Created by SZT on 2016/11/4.
//Copyright © 2016年SZT. All rights reserved.
//
importUIKit
@UIApplicationMain
classAppDelegate:UIResponder,UIApplicationDelegate{
varwindow:UIWindow?
funcapplication(application:UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) ->Bool{
letVC =ViewController()
letnavc =UINavigationController(rootViewController: VC)
window=UIWindow(frame:UIScreen.mainScreen().bounds)
window?.backgroundColor=UIColor.whiteColor()
window?.rootViewController= navc
window?.makeKeyAndVisible()
returntrue
}
funcapplicationWillResignActive(application:UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
funcapplicationDidEnterBackground(application:UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
funcapplicationWillEnterForeground(application:UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
funcapplicationDidBecomeActive(application:UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
funcapplicationWillTerminate(application:UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
//二、ViewController.swift
//
//ViewController.swift
//页面传值
//
//Created by SZT on 2016/11/4.
//Copyright © 2016年SZT. All rights reserved.
//
importUIKit
/*
界面通信:界面传值
(1)从前往后传
--属性传值
(2)从后往前传
--<1>代理传值,<2>closure(闭包)传值
*/
classViewController:UIViewController{
varlabel:UILabel?
overridefuncviewDidLoad() {
super.viewDidLoad()
label=UILabel(frame:CGRectMake(50,150,90,30))
label?.text="海贼王"
view.addSubview(label!)
navigationItem.title="FirstVC"
letbtn =UIButton(frame:CGRectMake(50,200,90,40))
btn.backgroundColor=UIColor.grayColor()
btn.setTitle("push", forState: .Normal)
btn.addTarget(self, action:"pushToSecondVC", forControlEvents: .TouchUpInside)
view.addSubview(btn)
}
funcpushToSecondVC(){
letsecondVC =SecondViewController()
//将当前的控制器设置为secondVC的代理
secondVC.closure= {
(str:String)->Void
in
self.label?.text= str
}
secondVC.delegate=self
//实例化
//传值
secondVC.labelStr=label?.text
navigationController?.pushViewController(secondVC, animated:true)
}
overridefuncdidReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//设置了代理就一定要遵循协议
extensionViewController:secondProtocol{
functranslateString(str:String) {
label?.text= str
}
}
//三、SecondViewController.swift
//
//SecondViewController.swift
//页面传值
//
//Created by SZT on 2016/11/4.
//Copyright © 2016年SZT. All rights reserved.
//
importUIKit
//从后往前传值的协议
protocolsecondProtocol{
//需要传什么类型的参数,参数列表就写什么
functranslateString(str:String)
}
classSecondViewController:UIViewController{
//定义labelStr属性,负责接收前一个页面传过来的值
varlabelStr:String?
vartextField:UITextField?
//定义代理属性
vardelegate:secondProtocol?
//定义闭包:
/*
let闭包名= {
(参数列表)->返回值类型
in
代码段
}
*/
//用来将该页面的文本框内容赋值到前一个页面的label
varclosure:((str:String)->(Void))?
overridefuncviewDidLoad() {
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"
letbtn =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)
letmodelBtn =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)
}
funcmodelToThird(){
letthirdVC =ThirdViewController()
presentViewController(thirdVC, animated:true, completion:nil)
}
funcpopToFirstVC(){
//代理传值
delegate?.translateString((textField?.text)!)
//(2)闭包传值
closure!(str: (textField?.text)!)
navigationController?.popViewControllerAnimated(true)
}
overridefuncdidReceiveMemoryWarning() {
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.
}
*/
}
//四、ThirdViewController.swift
//
//ThirdViewController.swift
//页面传值
//
//Created by SZT on 2016/11/4.
//Copyright © 2016年SZT. All rights reserved.
//
importUIKit
classThirdViewController:UIViewController{
overridefuncviewDidLoad() {
super.viewDidLoad()
view.backgroundColor=UIColor.greenColor()
letbtn =UIButton(frame:CGRectMake(50,200,90,40))
btn.backgroundColor=UIColor.grayColor()
btn.setTitle("dismiss", forState: .Normal)
btn.addTarget(self, action:"dismissToSecondVC", forControlEvents: .TouchUpInside)
view.addSubview(btn)
}
funcdismissToSecondVC(){
dismissViewControllerAnimated(true, completion:nil)
}
overridefuncdidReceiveMemoryWarning() {
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.
}
*/
}