objective-cswiftdelegatesclosuresobjective-c-swift-bridge

how to push a swift view controller from objective-c class and pass data back and continue the next process in objective c class


I want to push swift UIViewController from my objective C uiviewcontroller and wait for that viewcontroller to send back data. How can I do it.

I tried using protocol/Delegate in swift but it throws error as it is not available in objc .h file even if I'm importing "Pass-swift.h" bridging header file.

I am curious if there I can do with completion blocks if yes then how? or any other approach?

//Parentviewcontroller.m file


- (void)sendNextbutton:(id)sender{

UIViewController *vc = [[SecurityQuestionViewController alloc] init];

[self.navigationController pushViewController:vc animated:YES];

// I want below signup  function to calll after getting data back from vc viewcontoller
[self singupWithSecurityQuestion];}

// .h file

#import  "PassSDK-Swift.h"


@interface ParentViewController : UIViewController <MyProtocol>

@end

//SecurityQuestionViewController.swift

 @objc protocol MyProtocol {
   func setResultofSecurityQuestion(valueSent: [NSDictionary])
 }

@objc public class SecurityQuestionViewController: UIViewController
{    
  var delegate: MyProtocol?

@objc func didTapSubmitButton(sender: UIButton){
    let data: [NSDictionary] = getQuesDictionary()
    print(data)
    delegate?.setResultofSecurityQuestion(valueSent: data)
    navigationController?.popViewController(animated: true)
    dismiss(animated: true, completion: nil)
  }

}

//I should get data back to ParentViewController and it should wait for swift view controller to send back data and then it should go to next function/method - singupWithSecurityQuestion.


Solution

  • so the answer is that to use

           @objc protocol MyProtocol: class {
      func setResultofSecurityQuestion(valueSent: [NSDictionary])
              }
    

    and @objc everywhere