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地址:
2.1 如何在oc类中使用swift类
- #import "ocorswifttwo-swift.h"
- 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.
- }
上一篇: 新浪微博封面图不好看怎么自定义将封面图改为自己喜欢的
下一篇: 重新调整网络服务优先顺序的小技巧