Reputation: 789
I am trying to pass data back from Swift VC2 to ObjC VC1 using delegates while dismiss, below is my code.
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
How to assign the delegate in VC1 before moving to VC2 and how to implement protocol in VC1 (objc) file i.e. DataEnteredDelegate and the func userDidEnterInformation.
I believe I have completed the swift part of the task but looking for Objc implementation.
Upvotes: 0
Views: 738
Reputation: 139
First you need to create delegate instance initial with @objc
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)
}
}
Now in 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
Upvotes: 0
Reputation: 77700
Very basic example...
Assuming we have in Storyboard:
ObjcViewController
with a "Present It" buttonSwiftViewController
with a "Done" button, with Identifier: "SwiftVC"
and the buttons are connected to the IBAction
methods...
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)
}
}
Upvotes: 1