ain
copy
- 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.
- @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类
2.1.1 新建一个基于O
C语言的工程 ,取名 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"
- #import "OcOrSwiftTwo-swift.h"
2.2.2 在secondViewController.swift 中实现代理与闭包,代码如下:
注意: @objc(代理名) 才能在外部可见这个代理
- import UIKit
- import Foundation
-
- @objc(EditTextFieldDelegate)
- protocol Edi