iOS开发之OC与swift开发混编教程,代理的相互调用,block的实现。OC调用Swift中的代理, OC调用Swift中的Block 闭包
本文章将从两个方向分别介绍 OC 与 swift 混编
1. 第一个方向从 swift工程 中引入 oc类
1. 1 如何在swift的类中使用oc类
1.2 如何在swift中实现oc的代理方法
1.3 如何在swift中实现oc的Block回调
2 二个方向从OC工程中引入swift类
2.1 如何在OC类中使用swift类
2.2 如何在OC中实现swift的代理方法
2.3 如何在OC中实现swift中类似Block回调
下面是具体的实现过程:
1.1 如何在swift的类中使用oc类?
1. swift工程中引入OC类。 具体实现过程。
1.1 新建一个swift工程类。 取名 swiftOrOC
1.2 实现的功能为 : 从swift. viewController.swift 中 push到 OC语言 secondViewController 控制器
1.2.1 新建SecondViewController 类 。
1.2.2 建立桥接文件。 (很重要)
一定要记得点击这个按钮。
1.2.3 接下来工程目录如下:
1.2.4 接下来就可以实现具体的跳转功能了。
ViewController.swift中具体实现
import UIKit class ViewController: UIViewController { @IBOutlet weak var hintLabel: UILabel! //稍后用来显示回调 // push 到 oc controller
@IBAction func pushAction(_ sender: AnyObject) {
let secondVC = SecondViewController.init()
self.navigationController?.pushViewController(secondVC, animated: true)
} 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.
}
}
1.2 如何在swift中实现oc的代理方法
1.2.1 首先在 SecondViewController.h 中声明一个协议。具体代码
#import <UIKit/UIKit.h> @protocol SecondDelegate <NSObject> -(void)refreshHintLabel:(NSString *)hintString; @end @interface SecondViewController : UIViewController @property (nonatomic,weak)id<SecondDelegate> secondDelegate;
@end
1.2.3 接下来就非常简单了,让ViewController.swift只需要成为SecondViewController的代理,然后遵循她的协议,就可以了。 具体代码如下。
1.2.3.1 遵循协议
1.2.3.2 成为代理,并实现协议方法,更改controller.swift中hintLabel的text。
- // push 到 oc controller
- @IBAction func pushAction(_ sender: AnyObject) {
- let secondVC = SecondViewController.init()
- secondVC.secondDelegate = self;
- self.navigationController?.pushViewController(secondVC, animated: true)
- }
- // SecondViewControll的代理方法
- func refreshHintLabel(_ hintString: String!) {
- hintLabel.text = "secondView textView.text = " + hintString;
- }
1.3 如何在swift中实现oc的Block回调
1.3.1 具体过程与1.2小节一样。 直接上代码。
1.3.2 声明block;
- typedef void(^RefreshHintLabelBlock)(NSString *hintString);
- @interface SecondViewController : UIViewController
- @property (nonatomic, copy) RefreshHintLabelBlock hintBlock;
- @end
1.3.3 block的回调。 SecondViewController.m中
- #pragma mark 返回上一页回调 ,将用户输入的用户名传回给 ViewController.swift
- -(BOOL)navigationShouldPopOnBackButton{
- if (_hintBlock) {
- _hintBlock(textField.text);
- }
- return YES;
- }
1.3.4 在swift类中调用 oc的block.
- // push 到 oc controller
- @IBAction func pushAction(_ sender: AnyObject) {
- let secondVC = SecondViewController.init()
- secondVC.secondDelegate = self;
- secondVC.hintBlock = {(t:String?)in
- self.hintLabel.text = "secondView textView.text = " + t!
- }
- self.navigationController?.pushViewController(secondVC, animated: true)
- }
工程已上传到git上,git地址: https://github.com/zhonggaorong/SwiftOrOc/tree/master
2. OC工程中引入swift类。 具体实现过程。
耽误了不少时间, 今天才开始写oc工程中引入swift类。
demo地址:
https://github.com/jukai9316/OCtoSwift
2.1 如何在OC类中使用swift类
- #import "OcOrSwiftTwo-swift.h"
2.2.2 在secondViewController.swift 中实现代理与闭包,代码如下:
- import UIKit
- import Foundation
- // 必须加上@objc 代理才能在oc类中可见。
- @objc(EditTextFieldDelegate)
- protocol EditTextFieldDelegate:NSObjectProtocol {
- func editTextField(_ str: String) -> Void
- }
- @objc(SecondViewController)
- class SecondViewController: UIViewController {
- var editorDelegate:EditTextFieldDelegate?
- var textField:UITextField?
- var addButton:UIButton?
- var pushButton:UIButton?
- typealias editorBlock = (_ t:String) -> Void
- var myEidtorBlock:editorBlock?
- override func viewDidLoad() {
- super.viewDidLoad()
- self.view.backgroundColor = UIColor.white
- textField = UITextField.init(frame: CGRect.init(x: 50, y: 60, width: 200, height: 50))
- textField?.placeholder = "输入返回首页的内容"
- self.view.addSubview(textField!)
- addButton = UIButton.init(type: .custom)
- addButton?.setTitleColor(UIColor.black, for: .normal)
- addButton?.setTitle("pop", for: .normal)
- addButton?.frame = CGRect.init(x: 50, y: 150, width: 200, height: 50)
- addButton?.layer.borderColor = UIColor.black.cgColor
- addButton?.layer.borderWidth = 1.0
- addButton?.addTarget(self, action: #selector(popAction), for: .touchUpInside)
- self.view.addSubview(addButton!)
- pushButton = UIButton.init(type: .custom)
- pushButton?.setTitleColor(UIColor.black, for: .normal)
- pushButton?.setTitle("push", for: .normal)
- pushButton?.frame = CGRect.init(x: 50, y: 250, width: 200, height: 50)
- pushButton?.layer.borderColor = UIColor.black.cgColor
- pushButton?.layer.borderWidth = 1.0
- pushButton?.addTarget(self, action: #selector(pushAction), for: .touchUpInside)
- self.view.addSubview(pushButton!)
- }
- func popAction() -> Void {
- if editorDelegate != nil {
- editorDelegate?.editTextField((textField?.text)!)
- }
- if ((self.myEidtorBlock) != nil){
- self.myEidtorBlock!((textField?.text!)!)
- }
- self.navigationController?.popViewController(animated: true)
- }
- func pushAction() -> Void {
- let three = ThreeViewController.init()
- self.navigationController?.pushViewController(three, animated: true)
- }
- #import "ViewController.h"
- #import "OcOrSwiftTwo-swift.h"
- @interface ViewController ()<EditTextFieldDelegate>
- @property (nonatomic, strong) UITextField *showTextField;
- @property (nonatomic, strong) UIButton *pushButton;
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- _showTextField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100 , 200, 50)];
- _showTextField.placeholder = @"swift传回的文本内容";
- _showTextField.adjustsFontSizeToFitWidth = YES;
- _showTextField.enabled = NO;
- [self.view addSubview:_showTextField];
- _pushButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [_pushButton.layer setBorderColor:[UIColor blackColor].CGColor];
- [_pushButton.layer setBorderWidth:1.0];
- [_pushButton setFrame:CGRectMake(50, 200, 200, 50)];
- [_pushButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
- [_pushButton setTitle:@"push" forState:UIControlStateNormal];
- [_pushButton addTarget:self action:@selector(pushAction) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:_pushButton];
- }
- -(void)pushAction{
- SecondViewController *second = [[SecondViewController alloc]init];
- // second.editorDelegate = self;
- /*
- swift中的闭包回滴
- */
- second.myEidtorBlock = ^(NSString *str) {
- _showTextField.text = [NSString stringWithFormat:@"second传回信息: %@",str];
- };
- [self.navigationController pushViewController:second animated:YES];
- }
- #pragma mark swift中的代理
- -(void)editTextField:(NSString *)str{
- _showTextField.text = [NSString stringWithFormat:@"second传回信息: %@",str];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
iOS开发之OC与swift开发混编教程,代理的相互调用,block的实现。OC调用Swift中的代理, OC调用Swift中的Block 闭包的更多相关文章
- 李洪强iOS开发之iOS好文章收集
李洪强iOS开发之iOS好文章收集 该文收集朋友们转发或自己的写的技术文章,如果你也有相关的好文章,欢迎留言,当好文章多的时候,我会对这些好文章进行分门别类 文章 简述 日期 直播服务配置 使用 ng ...
- iOS开发之Socket通信实战--Request请求数据包编码模块
实际上在iOS很多应用开发中,大部分用的网络通信都是http/https协议,除非有特殊的需求会用到Socket网络协议进行网络数 据传输,这时候在iOS客户端就需要很好的第三方CocoaAsyncS ...
- iOS开发之info.pist文件和.pch文件
iOS开发之info.pist文件和.pch文件 如果你是iOS开发初学者,不用过多的关注项目中各个文件的作用.因为iOS开发的学习路线起点不在这里,这些文件只会给你学习带来困扰. 打开一个项目,我们 ...
- iOS开发之WKWebView简单使用
iOS开发之WKWebView简单使用 iOS开发之 WKWebVeiw使用 想用UIWebVeiw做的,但是突然想起来在iOS8中出了一个新的WKWebView,算是UIWebVeiw的升级版. ...
- 李洪强iOS开发之iOS社区收集
李洪强iOS开发之iOS社区收集 项目 简述 github 全球最大的代码仓库,无论是iOS开发还是Android开发没有人不知道这个网站,它也是一个社区,你可以去follow(关注)某些人或公司. ...
- 李洪强iOS开发之iOS工具收集
李洪强iOS开发之iOS工具收集 项目 简述 日期 我是怎么慢慢变懒的 : Jenkins + 蒲公英 使用Jenkins + 蒲公英使得项目打包给测试人员自动化,大大节省了劳动力 2015.04.1 ...
- 李洪强IOS开发之iOS好项目收集
李洪强IOS开发之iOS好项目收集 在这里收集一些最近出现的比较实用好玩的框架或者项目,会不断更新 项目 简述 日期 SCTableViewCell 类似与QQ侧滑删除Cell的Demo 201501 ...
- iOS开发之UISearchBar初探
iOS开发之UISearchBar初探 UISearchBar也是iOS开发常用控件之一,点进去看看里面的属性barStyle.text.placeholder等等.但是这些属性显然不足矣满足我们的开 ...
- iOS开发之UIImage等比缩放
iOS开发之UIImage等比缩放 评论功能真不错 评论开通后,果然有很多人吐槽.谢谢大家的支持和关爱,如果有做的不到的地方,还请海涵.毕竟我一个人的力量是有限的,我会尽自己最大的努力大家准备一些干货 ...
随机推荐
- Python档案袋( 面向对象 )
类即是一个模型,根据模型建立起不同的对象,对象间拥有共同的一些属性 简单的类: class P: #类变量,所有实例共享变量,推荐使用方法是:类名.类变量名 pvarx="ppvar1&qu ...
- 从搭建V2Ray服务器到编译V2Milk的完整过程
概述 因为公司出口ip一直在变,所以waf白名单不好加入,所以搭一个了代理服务器 .搭建了V2Ray服务器 .为了好管理,找了一个@Zzm317开源的V2Milk. V2Milk为V2Ray跨平台定制 ...
- 5.Git基础-撤销操作、标签的使用、Git别名
1.撤销操作 1.1 修改上一次的提交(commit)-- git commit --amend 有时候我们在提交完成之后才发现有几个文件没有提交,或者发现提交信息填写错了,这时候可以使用 git ...
- [Abp 源码分析]十三、多语言(本地化)处理
0.简介 如果你所开发的需要走向世界的话,那么肯定需要针对每一个用户进行不同的本地化处理,有可能你的客户在日本,需要使用日语作为显示文本,也有可能你的客户在美国,需要使用英语作为显示文本.如果你还是一 ...
- scala调用Linux命令行
在 scala 里面存在 调用 Linux 命令行的函数: import scala.sys.process._ 执行的方法也不难: import scala.sys.process._ /** * ...
- 并发编程(十六)——java7 深入并发包 ConcurrentHashMap 源码解析
以前写过介绍HashMap的文章,文中提到过HashMap在put的时候,插入的元素超过了容量(由负载因子决定)的范围就会触发扩容操作,就是rehash,这个会重新将原数组的内容重新hash到新的扩容 ...
- RabbitMQ学习笔记(六) RPC
什么RPC? 这一段是从度娘摘抄的. RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的 ...
- MySQL使用过程中的报错处理(持续更新)
一.数据库初始化 1.Percona的MySQL 5.6.20版本数据库初始化 初始化命令(MySQL 5.6版本不适用mysqld命令进行初始化) ./scripts/mysql_install_d ...
- Java实现将任何编码方式的txt文件以UTF-8编码方式转存
本文利用JDK中的BufferedReader和BufferedWriter实现将任何编码方式的txt文件以UTF-8编码方式转存. UTF-8(8-bit Unicode Transformatio ...
- selinux基本
TE模型 主体划分为若干组,称为域 客体划分为若干组,每个组称为一个类型 DDT(Domain Definition Table,域定义表,二维),表示域和类型的对应访问权限,权限包括读写执行 一 ...