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。

[objc] view plain copy
 
  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.   
  8. // SecondViewControll的代理方法  
  9. func refreshHintLabel(_ hintString: String!) {  
  10.     hintLabel.text = "secondView textView.text = " + hintString;  
  11. }  

 

 

 

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

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

 

        1.3.2 声明block;

         

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

 

 

        1.3.3 block的回调。 SecondViewController.m中

 

[objc] view plain copy
 
  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.

 

[objc] view plain copy
 
  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"
[objc] view plain copy
 
  1. #import "OcOrSwiftTwo-swift.h"  
   2.2.2 在secondViewController.swift 中实现代理与闭包,代码如下:
    注意: @objc(代理名)  才能在外部可见这个代理
 
[objc] view plain copy
 
  1. import UIKit  
  2. import Foundation  
  3.   
  4. // 必须加上@objc 代理才能在oc类中可见。  
  5. @objc(EditTextFieldDelegate)  
  6. protocol EditTextFieldDelegate:NSObjectProtocol {  
  7.     func editTextField(_ str: String) -> Void  
  8. }  
  9.   
  10. @objc(SecondViewController)  
  11. class SecondViewController: UIViewController {  
  12.   
  13.     var editorDelegate:EditTextFieldDelegate?  
  14.     var textField:UITextField?  
  15.     var addButton:UIButton?  
  16.     var pushButton:UIButton?  
  17.       
  18.     typealias editorBlock = (_ t:String) -> Void  
  19.     var myEidtorBlock:editorBlock?  
  20.       
  21.     override func viewDidLoad() {  
  22.         super.viewDidLoad()  
  23.         self.view.backgroundColor = UIColor.white  
  24.         textField = UITextField.init(frame: CGRect.init(x: 50, y: 60, width: 200, height: 50))  
  25.         textField?.placeholder = "输入返回首页的内容"  
  26.         self.view.addSubview(textField!)  
  27.           
  28.         addButton = UIButton.init(type: .custom)  
  29.         addButton?.setTitleColor(UIColor.black, for: .normal)  
  30.         addButton?.setTitle("pop", for: .normal)  
  31.         addButton?.frame = CGRect.init(x: 50, y: 150, width: 200, height: 50)  
  32.         addButton?.layer.borderColor = UIColor.black.cgColor  
  33.         addButton?.layer.borderWidth = 1.0  
  34.         addButton?.addTarget(self, action: #selector(popAction), for: .touchUpInside)  
  35.         self.view.addSubview(addButton!)  
  36.           
  37.           
  38.           
  39.         pushButton = UIButton.init(type: .custom)  
  40.         pushButton?.setTitleColor(UIColor.black, for: .normal)  
  41.         pushButton?.setTitle("push", for: .normal)  
  42.         pushButton?.frame = CGRect.init(x: 50, y: 250, width: 200, height: 50)  
  43.         pushButton?.layer.borderColor = UIColor.black.cgColor  
  44.         pushButton?.layer.borderWidth = 1.0  
  45.         pushButton?.addTarget(self, action: #selector(pushAction), for: .touchUpInside)  
  46.         self.view.addSubview(pushButton!)  
  47.           
  48.     }  
  49.       
  50.     func popAction() -> Void {  
  51.           
  52.         if editorDelegate != nil {  
  53.             editorDelegate?.editTextField((textField?.text)!)  
  54.         }  
  55.           
  56.         if ((self.myEidtorBlock) != nil){  
  57.             self.myEidtorBlock!((textField?.text!)!)  
  58.         }  
  59.           
  60.         self.navigationController?.popViewController(animated: true)  
  61.     }  
  62.       
  63.       
  64.     func pushAction() -> Void {  
  65.         let three = ThreeViewController.init()  
  66.         self.navigationController?.pushViewController(three, animated: true)  
  67.           
  68.     }       
    2.2.3   在oc类中viewcontroller.m 文件中实现SecondviewController.swift的相关代理与闭包(block). 代码如下:
   
[objc] view plain copy
 
  1. #import "ViewController.h"  
  2. #import "OcOrSwiftTwo-swift.h"  
  3.   
  4. @interface ViewController ()<EditTextFieldDelegate>  
  5. @property (nonatomic, strong) UITextField *showTextField;  
  6. @property (nonatomic, strong) UIButton *pushButton;  
  7.   
  8. @end  
  9.   
  10. @implementation ViewController  
  11.   
  12. - (void)viewDidLoad {  
  13.     [super viewDidLoad];  
  14.     _showTextField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100 , 200, 50)];  
  15.     _showTextField.placeholder = @"swift传回的文本内容";  
  16.     _showTextField.adjustsFontSizeToFitWidth = YES;  
  17.     _showTextField.enabled = NO;  
  18.     [self.view addSubview:_showTextField];  
  19.       
  20.     _pushButton = [UIButton buttonWithType:UIButtonTypeCustom];  
  21.     [_pushButton.layer setBorderColor:[UIColor blackColor].CGColor];  
  22.     [_pushButton.layer setBorderWidth:1.0];  
  23.     [_pushButton setFrame:CGRectMake(50, 200, 200, 50)];  
  24.     [_pushButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  
  25.     [_pushButton setTitle:@"push" forState:UIControlStateNormal];  
  26.     [_pushButton addTarget:self action:@selector(pushAction) forControlEvents:UIControlEventTouchUpInside];  
  27.       
  28.     [self.view addSubview:_pushButton];  
  29. }  
  30.   
  31.   
  32.   
  33.   
  34. -(void)pushAction{  
  35.     SecondViewController *second = [[SecondViewController alloc]init];  
  36.     // second.editorDelegate = self;  
  37.       
  38.     /* 
  39.       swift中的闭包回滴 
  40.      */  
  41.     second.myEidtorBlock = ^(NSString *str) {  
  42.         _showTextField.text = [NSString stringWithFormat:@"second传回信息: %@",str];  
  43.     };  
  44.     [self.navigationController pushViewController:second animated:YES];  
  45. }  
  46.   
  47. #pragma mark swift中的代理  
  48. -(void)editTextField:(NSString *)str{  
  49.     _showTextField.text = [NSString stringWithFormat:@"second传回信息: %@",str];  
  50. }  
  51.   
  52. - (void)didReceiveMemoryWarning {  
  53.     [super didReceiveMemoryWarning];  
  54.     // Dispose of any resources that can be recreated.  
  55. }  
posted @ 2018-11-06 16:37  鸿鹄当高远  阅读(5392)  评论(1编辑  收藏  举报