本文章将从两个方向分别介绍 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。

  1. // push 到 oc controller
  2. @IBAction func pushAction(_ sender: AnyObject) {
  3. let secondVC = SecondViewController.init()
  4. secondVC.secondDelegate = self;
  5. self.navigationController?.pushViewController(secondVC, animated: true)
  6. }
  7. // SecondViewControll的代理方法
  8. func refreshHintLabel(_ hintString: String!) {
  9. hintLabel.text = "secondView textView.text = " + hintString;
  10. }

1.3   如何在swift中实现oc的Block回调

1.3.1 具体过程与1.2小节一样。 直接上代码。

1.3.2 声明block;

  1. typedef void(^RefreshHintLabelBlock)(NSString *hintString);
  2. @interface SecondViewController : UIViewController
  3. @property (nonatomic, copy) RefreshHintLabelBlock hintBlock;
  4. @end

1.3.3 block的回调。 SecondViewController.m中

  1. #pragma mark 返回上一页回调 ,将用户输入的用户名传回给 ViewController.swift
  2. -(BOOL)navigationShouldPopOnBackButton{
  3. if (_hintBlock) {
  4. _hintBlock(textField.text);
  5. }
  6. return YES;
  7. }

1.3.4 在swift类中调用 oc的block.

  1. // push 到 oc controller
  2. @IBAction func pushAction(_ sender: AnyObject) {
  3. let secondVC = SecondViewController.init()
  4. secondVC.secondDelegate = self;
  5. secondVC.hintBlock = {(t:String?)in
  6. self.hintLabel.text = "secondView textView.text = " + t!
  7. }
  8. self.navigationController?.pushViewController(secondVC, animated: true)
  9. }

工程已上传到git上,git地址: https://github.com/zhonggaorong/SwiftOrOc/tree/master

2.  OC工程中引入swift类。 具体实现过程。

耽误了不少时间, 今天才开始写oc工程中引入swift类。

demo地址:

https://github.com/jukai9316/OCtoSwift

2.1  如何在OC类中使用swift类

 
       2.1.1   新建一个基于OC语言的工程 ,取名 OcOrSwiftTwo
       2.1. 2  实现的功能为 : 从oc类 viewcontroller中, push 至 swift语言 SecondViewController  ,然后SecondViewController可以通过代理或者swift闭包把值传回viewcontroller. 
       2.1.3   当前文件目录看下图:  (第四个箭头: 桥接文件)
        

  
    2.2   如何在OC中实现swift的代理与闭包Block方法
            
    2.2.1 如何在oc中引入swift类。#import "工程名-swift.h"
  1. #import "OcOrSwiftTwo-swift.h"

2.2.2 在secondViewController.swift 中实现代理与闭包,代码如下:

    注意: @objc(代理名)  才能在外部可见这个代理

  1. import UIKit
  2. import Foundation
  3. // 必须加上@objc 代理才能在oc类中可见。
  4. @objc(EditTextFieldDelegate)
  5. protocol EditTextFieldDelegate:NSObjectProtocol {
  6. func editTextField(_ str: String) -> Void
  7. }
  8. @objc(SecondViewController)
  9. class SecondViewController: UIViewController {
  10. var editorDelegate:EditTextFieldDelegate?
  11. var textField:UITextField?
  12. var addButton:UIButton?
  13. var pushButton:UIButton?
  14. typealias editorBlock = (_ t:String) -> Void
  15. var myEidtorBlock:editorBlock?
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. self.view.backgroundColor = UIColor.white
  19. textField = UITextField.init(frame: CGRect.init(x: 50, y: 60, width: 200, height: 50))
  20. textField?.placeholder = "输入返回首页的内容"
  21. self.view.addSubview(textField!)
  22. addButton = UIButton.init(type: .custom)
  23. addButton?.setTitleColor(UIColor.black, for: .normal)
  24. addButton?.setTitle("pop", for: .normal)
  25. addButton?.frame = CGRect.init(x: 50, y: 150, width: 200, height: 50)
  26. addButton?.layer.borderColor = UIColor.black.cgColor
  27. addButton?.layer.borderWidth = 1.0
  28. addButton?.addTarget(self, action: #selector(popAction), for: .touchUpInside)
  29. self.view.addSubview(addButton!)
  30. pushButton = UIButton.init(type: .custom)
  31. pushButton?.setTitleColor(UIColor.black, for: .normal)
  32. pushButton?.setTitle("push", for: .normal)
  33. pushButton?.frame = CGRect.init(x: 50, y: 250, width: 200, height: 50)
  34. pushButton?.layer.borderColor = UIColor.black.cgColor
  35. pushButton?.layer.borderWidth = 1.0
  36. pushButton?.addTarget(self, action: #selector(pushAction), for: .touchUpInside)
  37. self.view.addSubview(pushButton!)
  38. }
  39. func popAction() -> Void {
  40. if editorDelegate != nil {
  41. editorDelegate?.editTextField((textField?.text)!)
  42. }
  43. if ((self.myEidtorBlock) != nil){
  44. self.myEidtorBlock!((textField?.text!)!)
  45. }
  46. self.navigationController?.popViewController(animated: true)
  47. }
  48. func pushAction() -> Void {
  49. let three = ThreeViewController.init()
  50. self.navigationController?.pushViewController(three, animated: true)
  51. }
    2.2.3   在oc类中viewcontroller.m 文件中实现SecondviewController.swift的相关代理与闭包(block). 代码如下:

  1. #import "ViewController.h"
  2. #import "OcOrSwiftTwo-swift.h"
  3. @interface ViewController ()<EditTextFieldDelegate>
  4. @property (nonatomic, strong) UITextField *showTextField;
  5. @property (nonatomic, strong) UIButton *pushButton;
  6. @end
  7. @implementation ViewController
  8. - (void)viewDidLoad {
  9. [super viewDidLoad];
  10. _showTextField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100 , 200, 50)];
  11. _showTextField.placeholder = @"swift传回的文本内容";
  12. _showTextField.adjustsFontSizeToFitWidth = YES;
  13. _showTextField.enabled = NO;
  14. [self.view addSubview:_showTextField];
  15. _pushButton = [UIButton buttonWithType:UIButtonTypeCustom];
  16. [_pushButton.layer setBorderColor:[UIColor blackColor].CGColor];
  17. [_pushButton.layer setBorderWidth:1.0];
  18. [_pushButton setFrame:CGRectMake(50, 200, 200, 50)];
  19. [_pushButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  20. [_pushButton setTitle:@"push" forState:UIControlStateNormal];
  21. [_pushButton addTarget:self action:@selector(pushAction) forControlEvents:UIControlEventTouchUpInside];
  22. [self.view addSubview:_pushButton];
  23. }
  24. -(void)pushAction{
  25. SecondViewController *second = [[SecondViewController alloc]init];
  26. // second.editorDelegate = self;
  27. /*
  28. swift中的闭包回滴
  29. */
  30. second.myEidtorBlock = ^(NSString *str) {
  31. _showTextField.text = [NSString stringWithFormat:@"second传回信息: %@",str];
  32. };
  33. [self.navigationController pushViewController:second animated:YES];
  34. }
  35. #pragma mark swift中的代理
  36. -(void)editTextField:(NSString *)str{
  37. _showTextField.text = [NSString stringWithFormat:@"second传回信息: %@",str];
  38. }
  39. - (void)didReceiveMemoryWarning {
  40. [super didReceiveMemoryWarning];
  41. // Dispose of any resources that can be recreated.
  42. }

iOS开发之OC与swift开发混编教程,代理的相互调用,block的实现。OC调用Swift中的代理, OC调用Swift中的Block 闭包的更多相关文章

  1. 李洪强iOS开发之iOS好文章收集

    李洪强iOS开发之iOS好文章收集 该文收集朋友们转发或自己的写的技术文章,如果你也有相关的好文章,欢迎留言,当好文章多的时候,我会对这些好文章进行分门别类 文章 简述 日期 直播服务配置 使用 ng ...

  2. iOS开发之Socket通信实战--Request请求数据包编码模块

    实际上在iOS很多应用开发中,大部分用的网络通信都是http/https协议,除非有特殊的需求会用到Socket网络协议进行网络数 据传输,这时候在iOS客户端就需要很好的第三方CocoaAsyncS ...

  3. iOS开发之info.pist文件和.pch文件

    iOS开发之info.pist文件和.pch文件 如果你是iOS开发初学者,不用过多的关注项目中各个文件的作用.因为iOS开发的学习路线起点不在这里,这些文件只会给你学习带来困扰. 打开一个项目,我们 ...

  4. iOS开发之WKWebView简单使用

    iOS开发之WKWebView简单使用   iOS开发之 WKWebVeiw使用 想用UIWebVeiw做的,但是突然想起来在iOS8中出了一个新的WKWebView,算是UIWebVeiw的升级版. ...

  5. 李洪强iOS开发之iOS社区收集

    李洪强iOS开发之iOS社区收集 项目 简述 github 全球最大的代码仓库,无论是iOS开发还是Android开发没有人不知道这个网站,它也是一个社区,你可以去follow(关注)某些人或公司. ...

  6. 李洪强iOS开发之iOS工具收集

    李洪强iOS开发之iOS工具收集 项目 简述 日期 我是怎么慢慢变懒的 : Jenkins + 蒲公英 使用Jenkins + 蒲公英使得项目打包给测试人员自动化,大大节省了劳动力 2015.04.1 ...

  7. 李洪强IOS开发之iOS好项目收集

    李洪强IOS开发之iOS好项目收集 在这里收集一些最近出现的比较实用好玩的框架或者项目,会不断更新 项目 简述 日期 SCTableViewCell 类似与QQ侧滑删除Cell的Demo 201501 ...

  8. iOS开发之UISearchBar初探

    iOS开发之UISearchBar初探 UISearchBar也是iOS开发常用控件之一,点进去看看里面的属性barStyle.text.placeholder等等.但是这些属性显然不足矣满足我们的开 ...

  9. iOS开发之UIImage等比缩放

    iOS开发之UIImage等比缩放 评论功能真不错 评论开通后,果然有很多人吐槽.谢谢大家的支持和关爱,如果有做的不到的地方,还请海涵.毕竟我一个人的力量是有限的,我会尽自己最大的努力大家准备一些干货 ...

随机推荐

  1. Python档案袋( 面向对象 )

    类即是一个模型,根据模型建立起不同的对象,对象间拥有共同的一些属性 简单的类: class P: #类变量,所有实例共享变量,推荐使用方法是:类名.类变量名 pvarx="ppvar1&qu ...

  2. 从搭建V2Ray服务器到编译V2Milk的完整过程

    概述 因为公司出口ip一直在变,所以waf白名单不好加入,所以搭一个了代理服务器 .搭建了V2Ray服务器 .为了好管理,找了一个@Zzm317开源的V2Milk. V2Milk为V2Ray跨平台定制 ...

  3. 5.Git基础-撤销操作、标签的使用、Git别名

    1.撤销操作 1.1 修改上一次的提交(commit)--  git commit --amend 有时候我们在提交完成之后才发现有几个文件没有提交,或者发现提交信息填写错了,这时候可以使用 git ...

  4. [Abp 源码分析]十三、多语言(本地化)处理

    0.简介 如果你所开发的需要走向世界的话,那么肯定需要针对每一个用户进行不同的本地化处理,有可能你的客户在日本,需要使用日语作为显示文本,也有可能你的客户在美国,需要使用英语作为显示文本.如果你还是一 ...

  5. scala调用Linux命令行

    在 scala 里面存在 调用 Linux 命令行的函数: import scala.sys.process._ 执行的方法也不难: import scala.sys.process._ /** * ...

  6. 并发编程(十六)——java7 深入并发包 ConcurrentHashMap 源码解析

    以前写过介绍HashMap的文章,文中提到过HashMap在put的时候,插入的元素超过了容量(由负载因子决定)的范围就会触发扩容操作,就是rehash,这个会重新将原数组的内容重新hash到新的扩容 ...

  7. RabbitMQ学习笔记(六) RPC

    什么RPC? 这一段是从度娘摘抄的. RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的 ...

  8. MySQL使用过程中的报错处理(持续更新)

    一.数据库初始化 1.Percona的MySQL 5.6.20版本数据库初始化 初始化命令(MySQL 5.6版本不适用mysqld命令进行初始化) ./scripts/mysql_install_d ...

  9. Java实现将任何编码方式的txt文件以UTF-8编码方式转存

    本文利用JDK中的BufferedReader和BufferedWriter实现将任何编码方式的txt文件以UTF-8编码方式转存. UTF-8(8-bit Unicode Transformatio ...

  10. selinux基本

    TE模型 主体划分为若干组,称为域 客体划分为若干组,每个组称为一个类型   DDT(Domain Definition Table,域定义表,二维),表示域和类型的对应访问权限,权限包括读写执行 一 ...