我试图在关闭时使用委托将资料从 Swift VC2 传递回 ObjC VC1,下面是我的代码。
VC2(迅捷)
//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。
我相信我已经完成了任务的快速部分,但正在寻找 Objc 实作。
uj5u.com热心网友回复:
非常基本的例子...
假设我们在 Storyboard 中有:
ObjcViewController
带有“Present It”按钮的 Objective-C 控制器- 一个
SwiftViewController
带有“完成”按钮的 Swift 控制器,带有Identifier: "SwiftVC"
并且按钮连接到IBAction
方法...
ObjcViewController.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface ObjcViewController : UIViewController
@end
NS_ASSUME_NONNULL_END
物件视图控制器.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)
}
}
uj5u.com热心网友回复:
首先,您需要使用 @objc 创建委托实体初始值
VC2(迅捷)
//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
0 评论