IOS开发基础06(界面通信(界面传值):属性传值、代理传值、闭包传值)
2016-11-07 14:23
363 查看
// AppDelegate.swift
import UIKit
@UIApplicationMain
class AppDelegate:
UIResponder, UIApplicationDelegate {
var window:
UIWindow?
func application(application:
UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:
AnyObject]?) -> Bool {
//设置导航视图控制器
let vc =
ViewController()
let navc =
UINavigationController(rootViewController: vc)
window =
UIWindow(frame: UIScreen.mainScreen().bounds)
window?.backgroundColor =
UIColor.whiteColor()
window?.rootViewController = navc
window?.makeKeyAndVisible()
return
true
}
func applicationWillResignActive(application:
UIApplication) {
}
func applicationDidEnterBackground(application:
UIApplication) {
}
func applicationWillEnterForeground(application:
UIApplication) {
}
func applicationDidBecomeActive(application:
UIApplication) {
}
func applicationWillTerminate(application:
UIApplication) {
}
}
// ViewController.swift
import UIKit
/*
界面通信:界面传值
1.从前往后传
——属性传值
2.从后往前传
——1.代理传值
——2.closure(闭包)传值
*/
class ViewController:
UIViewController {
var label0:UILabel?
override
func viewDidLoad() {
super.viewDidLoad()
navigationItem.title =
"VC01"
label0 =
UILabel(frame: CGRectMake(140,
150, 100,
40))
label0!.text =
"死神"
label0!.textColor =
UIColor.blueColor()
label0!.textAlignment = .Center
view.addSubview(label0!)
let nextBtn =
UIBarButtonItem(title: "next", style:
UIBarButtonItemStyle.Plain, target:
self, action: "nextAction")
navigationItem.rightBarButtonItem = nextBtn
}
func nextAction(){
let secondVC =
SecondViewController()
//给sencondVC定义具体的闭包方法(从后往前————闭包传值)
secondVC.color = {
(str:UIColor) ->
Void
in
self.view.backgroundColor = str
}
//将当前的控制器设置为secondVC的代理(从后往前————代理传值)
secondVC.delegate =
self
//传值(从前往后————属性传值)
secondVC.labelStr =
label0?.text
navigationController?.pushViewController(secondVC, animated:
true)
}
override
func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
//设置了代理就一定要遵循协议
extension ViewController:secondPro {
func translateString(str:
String) {
label0?.text = str
}
}
// SecondViewController.swift
import UIKit
//从后往前传值的协议
protocol secondPro {
//需要传什么类型的参数,参数列表就写什么
func translateString(str:String)
}
class SecondViewController:
UIViewController {
//定义一个闭包
var color:((str:UIColor)->Void)?
//定义labelStr属性负责接受来自前一个页面传过来的值
var labelStr:String?
var textField:UITextField?
//定义代理属性
var delegate:secondPro?
override
func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor =
UIColor.whiteColor()
navigationItem.title =
"VC02"
let backBtn =
UIBarButtonItem(title: "<back", style:
UIBarButtonItemStyle.Plain, target:
self, action: "backAction")
navigationItem.leftBarButtonItem = backBtn
textField =
UITextField(frame: CGRectMake(100,
100, 200,
40))
textField!.placeholder =
"请输入文字"
textField!.borderStyle = .RoundedRect
textField?.text =
labelStr
view.addSubview(textField!)
}
func backAction(){
//调用闭包方法
let str =
UIColor.blueColor()
color!(str:str)
//调用协议
delegate?.translateString((textField?.text)!)
navigationController?.popViewControllerAnimated(true)
}
override
func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
import UIKit
@UIApplicationMain
class AppDelegate:
UIResponder, UIApplicationDelegate {
var window:
UIWindow?
func application(application:
UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:
AnyObject]?) -> Bool {
//设置导航视图控制器
let vc =
ViewController()
let navc =
UINavigationController(rootViewController: vc)
window =
UIWindow(frame: UIScreen.mainScreen().bounds)
window?.backgroundColor =
UIColor.whiteColor()
window?.rootViewController = navc
window?.makeKeyAndVisible()
return
true
}
func applicationWillResignActive(application:
UIApplication) {
}
func applicationDidEnterBackground(application:
UIApplication) {
}
func applicationWillEnterForeground(application:
UIApplication) {
}
func applicationDidBecomeActive(application:
UIApplication) {
}
func applicationWillTerminate(application:
UIApplication) {
}
}
// ViewController.swift
import UIKit
/*
界面通信:界面传值
1.从前往后传
——属性传值
2.从后往前传
——1.代理传值
——2.closure(闭包)传值
*/
class ViewController:
UIViewController {
var label0:UILabel?
override
func viewDidLoad() {
super.viewDidLoad()
navigationItem.title =
"VC01"
label0 =
UILabel(frame: CGRectMake(140,
150, 100,
40))
label0!.text =
"死神"
label0!.textColor =
UIColor.blueColor()
label0!.textAlignment = .Center
view.addSubview(label0!)
let nextBtn =
UIBarButtonItem(title: "next", style:
UIBarButtonItemStyle.Plain, target:
self, action: "nextAction")
navigationItem.rightBarButtonItem = nextBtn
}
func nextAction(){
let secondVC =
SecondViewController()
//给sencondVC定义具体的闭包方法(从后往前————闭包传值)
secondVC.color = {
(str:UIColor) ->
Void
in
self.view.backgroundColor = str
}
//将当前的控制器设置为secondVC的代理(从后往前————代理传值)
secondVC.delegate =
self
//传值(从前往后————属性传值)
secondVC.labelStr =
label0?.text
navigationController?.pushViewController(secondVC, animated:
true)
}
override
func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
//设置了代理就一定要遵循协议
extension ViewController:secondPro {
func translateString(str:
String) {
label0?.text = str
}
}
// SecondViewController.swift
import UIKit
//从后往前传值的协议
protocol secondPro {
//需要传什么类型的参数,参数列表就写什么
func translateString(str:String)
}
class SecondViewController:
UIViewController {
//定义一个闭包
var color:((str:UIColor)->Void)?
//定义labelStr属性负责接受来自前一个页面传过来的值
var labelStr:String?
var textField:UITextField?
//定义代理属性
var delegate:secondPro?
override
func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor =
UIColor.whiteColor()
navigationItem.title =
"VC02"
let backBtn =
UIBarButtonItem(title: "<back", style:
UIBarButtonItemStyle.Plain, target:
self, action: "backAction")
navigationItem.leftBarButtonItem = backBtn
textField =
UITextField(frame: CGRectMake(100,
100, 200,
40))
textField!.placeholder =
"请输入文字"
textField!.borderStyle = .RoundedRect
textField?.text =
labelStr
view.addSubview(textField!)
}
func backAction(){
//调用闭包方法
let str =
UIColor.blueColor()
color!(str:str)
//调用协议
delegate?.translateString((textField?.text)!)
navigationController?.popViewControllerAnimated(true)
}
override
func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
相关文章推荐
- 界面通信之属性传值、代理传值
- ios开发-UI基础-应用管理(单纯界面)改进5-使用代理实现监听下载按钮的点击(delegate)
- 10 导航控制器UINavigationController、导航条navigationBar、界面通信(属性传值、代理传值、单例传值)
- iOS中的导航条UINavigationController(UISegmentedControl)的基本使用(界面中传值的3中方法,单例,属性,代理)
- iOS个人整理14-界面间传值:属性传值---代理传值---block传值---单例传值
- IOS学习笔记(页面传值:属性传值,协议代理传值,闭包传值)
- IOS界面通信-代理(协议)传值
- iOS开发:界面传值之属性和代理传值
- iOS 界面间的传值 属性传值 代理传值
- iOS史上最全界面跳转传值总结及分析-包括属性传值、代理传值、通知传值、单例传值、代码块传值
- iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)
- Objective-C ,ios,iphone开发基础:多个视图(view)之间的切换2,使用导航栏控制,以及视图之间传值。
- iOS开发- 界面传值(1)-通知模式(广播)
- IOS应用开发07——城市定位及城市列表选取(代理反向传值)
- iOS开发- 界面传值(1)-通知模式(广播)
- iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值) 推荐
- iOS开发UI基础—手写控件,frame,center和bounds属性
- iOS开发:界面之间传值之Block传值(Block高级用法:Block传值)
- [置顶] Objective-C ,ios,iphone开发基础:protocol 协议(委托,代理)的声明
- Objective-C ,ios,iphone开发基础:多个视图(view)之间的切换2,使用导航栏控制,以及视图之间传值。