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

DroidDev

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.

DonMag

Very basic example...

Assuming we have in Storyboard:

  • an Objective-C controller ObjcViewController with a "Present It" button
  • a Swift controller SwiftViewController 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)
    }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Unable to pass a value from the child VC to the parent VC using protocol/delegate

How to edit and pass back cell data with delegate and protocol in swift

How do I pass data from one VC to another using closures in swift instead of delegates?

Optional Protocol methods in swift without using @objc

iOS Swift - Protocol sending delegate to wrong TableViewController

How can I set the delegate for a protocol from the child of the destination VC?

Implement delegate using generic protocol in Swift

ObjC protocol Implementation in Swift

how to pass data in perform segue sender through protocol & delegate from a UICollectionViewCell inside UITableViewCell?

Pass data by protocol while using Container to view another ViewController in Swift

I cannot able to pass data with using protocol in swift

Inherit from @objc protocol

iOS Protocol / Delegate confusion?

I can't use Delegate and Protocol to pass data

Value pass from delegate method is nil Swift

@objc protocol crashes the swift compiler

Extend @objc protocol with Comparable in Swift

Extend @objc protocol with Hashable in Swift

How to Pass Data Multiple Times from Parent View Controller to Container VC Child in Swift?

using swift extension from objc, and type difference

How to pass a generic decodable data using a delegate

Xcode using delegate to pass data between controllers

Pass data from detailview to masterview with delegate not working

Swift expose the protocol to objc in protocol oriented programming

Swift protocol implementing another @objc protocol

Access delegate protocol from Swift framework into Objective C

Swift - Protocol Delegate from TableView not working - Present Modally

Swift Protocol Third VC to First VC

Passing data between 2 UIViewController using delegate and protocol