美文网首页
11.4页面传值

11.4页面传值

作者: 冰凡513 | 来源:发表于2016-11-04 11:10 被阅读0次

//一、AppDelegate.swift

//

//AppDelegate.swift

//页面传值

//

//Created by SZT on 2016/11/4.

//Copyright © 2016SZT. 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 © 2016SZT. 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 © 2016SZT. 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 © 2016SZT. 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.

}

*/

}

相关文章

  • 11.4页面传值

    //一、AppDelegate.swift // //AppDelegate.swift //页面传值 // //...

  • iOS 传值

    页面传值 NSNotification Delegate Block 单例 一、 页面传值 最简单直接的传值方法 ...

  • ioS 页面(代理、通知、block、单例、属性)传值

    iOS 页面(代理、通知、block、单例、属性)传值 一、传值分类 页面传值基本分为两种:正向传值和反向传值。 ...

  • iOS 页面(代理、通知、block、单例、属性)传值

    iOS 页面(代理、通知、block、单例、属性)传值 一、传值分类 页面传值基本分为两种:正向传值和反向传值。 ...

  • 界面通信

    //控制器1 importUIKit //页面传值1:从前往后传 //页面传值2:(1)闭包传值 //(2)代理传...

  • this.setData赋值不成功的原因之一

    父页面根据成功与否传值不同的值到子页面:成功传值:status=1, orderData={对象}失败传值:sta...

  • Vue页面间传值

    Vue传值的方式有正向传值和反向传值 正向传值:上一页面 --> 下一页面 路由传值 本地储存 eventbus ...

  • swift 代理传值

    场景:A页面跳转到B页面,B页面返回到A页面,(B页面给A页面传值) B页面逻辑:创建协议,声明变量,传值 A页面...

  • swift 闭包传值

    场景:A页面跳转到B页面,B页面返回到A页面,(B页面给A页面传值) B页面逻辑:创建block,声明变量,传值 ...

  • 微信小程序 页面传值详解

    微信小程序 页面传值详解 一. 跨页面传值. 1 . 用 navigator标签传值或 wx.navigator,...

网友评论

      本文标题:11.4页面传值

      本文链接:https://www.haomeiwen.com/subject/jloiuttx.html