0

I have created my custom dialog in swift with xib file Now i want to use that dialog from objective c file I am able to show the dialog but i cannot able to listen click event of button

My Swift code is like

public class CustomVerificationDialog:UIView,UITextFieldDelegate
{
     ///Othere stuffs
      var onClickRightButton : ((_ text1:String , _ text2:String ) -> Void)? = nil //This method is called when button is clicked

     @IBAction func onClickBtnRight(_ sender: Any)
        {
            if self.onClickRightButton != nil
            {
                self.onClickRightButton!(editTextOne.text ?? "",editTextTwo.text ?? "")
            }

        }
 }

Now i am able to get the click event in swift like

dialogForVerification.onClickRightButton =
            { (text1:String,text2:String) in
                }

But i dont know how to listen it in objective c

CustomVerificationDialog *dialogVerification =  [CustomVerificationDialog showCustomDialog];
???

2 Answers 2

1

You can try

[dialogVerification setOnClickRightButton:^(NSString * _Nonnull text1 , NSString * _Nonnull text2 ) {

}];

Here is a Demo

Sign up to request clarification or add additional context in comments.

3 Comments

No its not working it says No visible @interface for 'CustomVerificationDialog' declares the selector 'setOnClickRightButton:'
Did you tested it ???? set is appended for properties , see the attached demo
Yes its working after clean and rebuild the project. first time its show me the compile time error but after rebuild error goes thanks Sh_Khan..
0

In order to access any swift class and its methods/properties in objective-c file, you need to mark that class/method/property with @objc. Then you need to import "TargetName-Swift.h" in your objective-c file in order to access swift code in objective-c file. Here is the code snippet which hopefully solve your problem:

Replace your swift class with:

    @objc public class CustomVerificationDialog:UIView,UITextFieldDelegate

    {

        @objc var onClickRightButton : ((_ text1:String , _ text2:String ) -> Void)? = nil //This method is called when button is clicked

        @IBAction func onClickBtnRight(_ sender: Any)
        {
           if self.onClickRightButton != nil
           {
           self.onClickRightButton!(editTextOne.text ?? "",editTextTwo.text ?? "")
           }

        }
        //other code goes here
    }

and In your objective-c file (i.e. viewController)

#import "YourTargetName-Swift.h"

...................
................
CustomVerificationDialog *dialogVerification =  [CustomVerificationDialog showCustomDialog];
[dialogVerification setOnClickRightButton:^(NSString * _Nonnull text1 , NSString * _Nonnull text2 ) {

}];
// other code here
.................
..................

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.