swift-delegate(代理)或者block传值
2024-08-31 01:48:45
1:delegate或者block传值
import UIKit
class ViewController: UIViewController,TestDelegatePassValueDelegate { @IBOutlet weak var TFContent: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} @IBAction func delegateOrBlockBtnClick(sender: AnyObject) {
TFContent.text = ""
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let testVCtl = storyBoard.instantiateViewControllerWithIdentifier("TestDelegateOrBlockViewController") as!TestDelegateOrBlockViewController;
/**设置代理*/
testVCtl.delegate = self
/**block回调传值*/
testVCtl.passValueUseBlock { (valueString) -> Void in self.TFContent!.text = valueString as String }
self.navigationController!.pushViewController(testVCtl, animated: true); }
/**代理函数*/
func passValueUseDelegate(valueString: NSString)
{
self.TFContent!.text = valueString as String
} }
import UIKit
import Foundation
/**定义一个block*/
typealias TestBlockCompleteHandler = (valueString: NSString)->Void
/**声明定义协议*/
protocol TestDelegatePassValueDelegate:NSObjectProtocol
{
func passValueUseDelegate(valueString: NSString) } class TestDelegateOrBlockViewController: UIViewController
{ @IBOutlet weak var TFContent: UITextField!
var delegate:TestDelegatePassValueDelegate? var myBlockHandler:TestBlockCompleteHandler?
override func viewDidLoad() {
super.viewDidLoad() // Do any additional setup after loading the view.
} override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} //MARK: - 用代理来实现传值
@IBAction func delegateBtnClick(sender: AnyObject)
{ TFContent.resignFirstResponder()
if ((delegate != nil) && (delegate?.respondsToSelector("passValueUseDelegate:") != nil))
{
delegate?.passValueUseDelegate(TFContent.text!) }
self.navigationController?.popViewControllerAnimated(true) } //MARK: - 用block来实现传值
@IBAction func blockBtnClick(sender: AnyObject)
{ TFContent.resignFirstResponder()
myBlockHandler?(valueString: self.TFContent.text!)
self.navigationController?.popViewControllerAnimated(true) } func passValueUseBlock(blockHandler: TestBlockCompleteHandler)
{
myBlockHandler = blockHandler }
最新文章
- bzoj3732: Network--kruskal最小生成树+LCA
- Android--Retrofit+RxJava的简单封装(三)
- WCF配置工具及服务调试工具
- sqlserver中索引优化
- HDU 5706 GirlCat (DFS,暴力)
- Mac下kernel_task占用大量CPU怎么办?
- c编写程序完成m名旅客和n辆汽车的同步程序代写
- linux umount 提示device is busy 的解决
- jvm常量池 vsv为什么1000 == 1000返回为False,而100 == 100会返回为True?
- poj 1087.A Plug for UNIX (最大流)
- nova-network
- JDK5什么是新的线程锁技术(两)
- spring mvc 注解入门示例
- NodeJS 常用模块积累
- PHP foreach 循环
- windows(xshell)免密码登录
- 第一天Python
- Dota 2 中安装包的作用
- POSTMAN模拟http请求
- 关于face alglimnment各种资料,存下来有空慢慢看
热门文章
- PAT 1060. Are They Equal
- 《hello-world》第八次团队作业:Alpha冲刺-Scrum Meeting 4
- Vue项目搭建及原理四
- ansible使用jinja2管理配置文件以及jinja2语法简介
- [cf 599C] Day at the Beach
- poj1330 lca 最近公共祖先问题学习笔记
- 装饰器 转载自 http://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html
- iOS:改变UITableViewCell的选中背景色
- PyQt5学习随笔01--计算一个目录里我们码的代码行数&;amp;&;amp;PyQt的多线程通信
- java电影站点开发经验3