iOS - 使用协议和委托将数据从 Swift 传递到 ObjC VC

iOS - Pass Data from Swift to ObjC VC using protocol and delegate

我试图在关闭时使用委托将数据从 Swift VC2 传回 ObjC VC1,下面是我的代码。

VC2 (swift)

//protocol used for sending data back
@objc protocol DataEnteredDelegate: AnyObject {
    func userDidEnterInformation(info: String)
}

class VC2: UIViewController {

    weak var delegate: DataEnteredDelegate? = nil

    // **Go back to VC1**
    @IBAction func doneBtn(_ sender: Any) { 
      session.stop();
      delegate!.userDidEnterInformation(info: "sending date from Swift VC2")
      self.dismiss(animated: true, completion: nil) 
    }
}

VC1.m (objc)

#import "ProjectName-Swift.h"

@interface VC1 () <>
 
@end

@implementation VC1
// **Navigate to VC2**
-(void)goToVC2 {
    VC2 *vc2 = [[UIStoryboard storyboardWithName: @"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"VC2ID"];
    [self presentViewController: vc2 animated: YES completion: nil];
}
@end

VC1.h (objc)

@interface VC1 : UIViewController

@end

如何在移动到 VC2 之前在 VC1 中分配委托以及如何在 VC1 (objc) 文件中实现协议,即 DataEnteredDelegate 和 func userDidEnterInformation。

我相信我已经完成了任务的 swift 部分,但正在寻找 Objc 实现。

非常基本的例子...

假设我们在 Storyboard 中有:

  • 带有“Present It”按钮的 Objective-C 控制器 ObjcViewController
  • 一个 Swift 控制器 SwiftViewController 带有“完成”按钮,Identifier: "SwiftVC"

并且按钮连接到 IBAction 方法...

ObjcViewController.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ObjcViewController : UIViewController

@end

NS_ASSUME_NONNULL_END

ObjcViewController.m

#import "ObjcViewController.h"

#import "YourProject-Swift.h"

@interface ObjcViewController () <DataEnteredDelegate>

@end

@implementation ObjcViewController

- (IBAction)goToSwiftVC:(id)sender {
    
    SwiftViewController *vc = (SwiftViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"SwiftVC"];
    [vc setMyDelegate:self];
    [self presentViewController:vc animated:YES completion:nil];
    
}

- (void)userDidEnterInformationWithInfo:(NSString * _Nonnull)info {
    NSLog(@"Delegate sent back: %@", info);
}

@end

SwiftViewController.swift

import UIKit

@objc public protocol DataEnteredDelegate {
    func userDidEnterInformation(info: String)
}

class SwiftViewController: UIViewController {

    @objc public var myDelegate: DataEnteredDelegate?
    
    @IBAction func doneBtn(_ sender: Any) {
        myDelegate?.userDidEnterInformation(info: "from SwiftVC")
        self.dismiss(animated: true, completion: nil)
    }

}

首先你需要用@objc

创建委托实例initial

VC2 (swift)

//protocol used for sending data back
@objc protocol DataEnteredDelegate: AnyObject {
    func userDidEnterInformation(info: String)
}

class VC2: UIViewController {

   @objc weak var delegate: DataEnteredDelegate?

    // **Go back to VC1**
    @IBAction func doneBtn(_ sender: Any) { 
      session.stop();
      delegate!.userDidEnterInformation(info: "sending date from Swift VC2")
      self.dismiss(animated: true, completion: nil) 
    }
} 

现在 objective-c,VC1.m (objc)

#import "ProjectName-Swift.h"

@interface VC1 () <DataEnteredDelegate>
 
@end

@implementation VC1
// **Navigate to VC2**
-(void)goToVC2 {
    VC2 *vc2 = [[UIStoryboard storyboardWithName: @"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"VC2ID"];
    [vc2 delegate:self];
    [self presentViewController: vc2 animated: YES completion: nil];
}

//On dismiss vc2 this delegate will call
- (void)userDidEnterInformationWithInfo:(NSString *)info{
    NSLog(@"%@", info);
}
@end