Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DidTapCheckBox is not called #13

Closed
omatrot opened this issue May 10, 2020 · 13 comments
Closed

DidTapCheckBox is not called #13

omatrot opened this issue May 10, 2020 · 13 comments
Assignees

Comments

@omatrot
Copy link

@omatrot omatrot commented May 10, 2020

I'm using version 1.4.3.
Xamarin.iOS
Version: 13.18.1.31 (Visual Studio Community)
Hash: b3eedfed9
Branch: d16-6
Build date: 2020-04-09 10:22:36-0400

Neither DidTapCheckBox nor AnimationDidStopForCheckBox subscription are working.
The event method is not called.

BEMCheckBox checkbox = new BEMCheckBox { TranslatesAutoresizingMaskIntoConstraints = false };
// Wire the check event
checkbox.DidTapCheckBox += Checkbox_DidTapCheckBox;
...

private void Checkbox_DidTapCheckBox(object sender, EventArgs e)
{
    throw new NotImplementedException();
}

What could be wrong?

@mrbiggred mrbiggred self-assigned this May 12, 2020
@mrbiggred
Copy link
Member

@mrbiggred mrbiggred commented May 12, 2020

@omatrot thank you for bringing this issues to my attention. I'll take a look at your issue this week and let you know what I find out. If you have any other information about DidTapCheckBox not working or fix the problem please let me know.

@mrbiggred
Copy link
Member

@mrbiggred mrbiggred commented May 12, 2020

@omatrot I just loaded the example application, which I haven't done in a while, and the DidTapCheckBox event is firing. I also created a new Xamarin iOS application in Visual Studio 2019, added a the BEM Checkbox via Nuget, and the DidTapCheckBox worked as expected.

Can you please provide more details on your setup and/or an example that demonstrates the bug? Thank you.

@omatrot
Copy link
Author

@omatrot omatrot commented May 12, 2020

Here's what I'm doing:
I'm creating 5 checkboxes and add them to an horizontal StackView

UIStackView firstFiveCheckboxesStackView = new UIStackView
                    {
                        TranslatesAutoresizingMaskIntoConstraints = false,
                        Axis = UILayoutConstraintAxis.Horizontal,
                        Distribution = UIStackViewDistribution.EqualSpacing,
                        Alignment = UIStackViewAlignment.Fill,
                        Spacing = 0
                    };

                    UIView spacerView = null;
                    for (int i = 0; i < kv.Value.count && i < 5; i++)
                    {
                        // Create the first spacer and add it to the stackview
                        spacerView = new UIView();
                        firstFiveCheckboxesStackView.AddArrangedSubview(spacerView);
                        // Create the checkbox and add it to the StackView
                        BEMCheckBox checkbox = new BEMCheckBox { TranslatesAutoresizingMaskIntoConstraints = false };
                        // Wire the check event
                        checkbox.DidTapCheckBox -= habitDoneState_Click;
                        checkbox.DidTapCheckBox += habitDoneState_Click;
                        // Define properties
                        checkbox.HeightAnchor.ConstraintEqualTo(24).Active = true;
                        checkbox.WidthAnchor.ConstraintEqualTo(24).Active = true;
                        checkbox.BoxType = BEMBoxType.Circle;
                        checkbox.OnAnimationType = BEMAnimationType.Stroke;
                        // Transparent BackgroundColor
                        checkbox.BackgroundColor = null;
                        checkbox.TintColor = UIColor.Gray;
                        checkbox.OnTintColor = checkbox.OnCheckColor = optimistic_orange;

                        // Add the checkbox to the StackView
                        firstFiveCheckboxesStackView.AddArrangedSubview(checkbox);
                    }
                    // Create the last spacer and add it to the stackview
                    spacerView = new UIView();
                    firstFiveCheckboxesStackView.AddArrangedSubview(spacerView);

Hope that helps, my project is quite complicated I'm not sure I can provide a small repo sample. If the above code works for you then I'll try to reproduce in a fork of the sample project.

@mrbiggred
Copy link
Member

@mrbiggred mrbiggred commented May 12, 2020

@omatrot I replaced the MainViewController ViewDidLoad method with the below code and everything worked as expected.

public override void ViewDidLoad()
{
	base.ViewDidLoad();

	Title = "Binding Example";

	var stackView = new UIStackView
	{
		Axis = UILayoutConstraintAxis.Horizontal,
		Distribution = UIStackViewDistribution.EqualSpacing,
		Alignment = UIStackViewAlignment.Center,
		Spacing = 5,
		TranslatesAutoresizingMaskIntoConstraints = false
	};

	for (int i = 0; i < 5; i++)
	{
		var checkbox = new BEMCheckBox(new CGRect(0, 0, 25, 25));
		checkbox.DidTapCheckBox += DidTapCheckBoxEvent;
		checkbox.AnimationDidStopForCheckBox += AnimationDidStopForCheckBoxEvent;

		stackView.AddArrangedSubview(checkbox);
	}

	View.AddSubview(stackView);

	stackView.CenterXAnchor.ConstraintEqualTo(View.CenterXAnchor).Active = true;
	stackView.CenterYAnchor.ConstraintEqualTo(View.CenterYAnchor).Active = true;
}

Try the above and let me know if it works. Could be something else I'm missing or something you haven't shown me yet.

I did notice that you remove then re-add the DidTapCheckBox event:

checkbox.DidTapCheckBox -= habitDoneState_Click;
checkbox.DidTapCheckBox += habitDoneState_Click;

I don't think you need to do this. Just add it:

checkbox.DidTapCheckBox += habitDoneState_Click;

@omatrot
Copy link
Author

@omatrot omatrot commented May 14, 2020

Can't seem to solve the problem in my project neither reproduce it in a new one. Please stay tuned.

@omatrot
Copy link
Author

@omatrot omatrot commented May 16, 2020

May be the problem is in the native library, and in this case I have no idea if we could debug it. The only thing that comes to mind is that the delegate in no more registered. Unless we have a BEMCheckbox native version that logs if there is no delegate to call we'll never know.
I'll try to use my own tap gesture responder in a derived class.

@mrbiggred
Copy link
Member

@mrbiggred mrbiggred commented May 19, 2020

Adding the below email from @omatrot to help with debugging this issue:

All right solved it by using the constructor that takes a CGRect to set the desired size, instead of the default constructor and using explicit constraints for height and width (as you can see in the above code or below commented code).

So this becomes:

                    BEMCheckBox checkbox = new BEMCheckBox(new CoreGraphics.CGRect(0, 0,24, 24)); // { TranslatesAutoresizingMaskIntoConstraints = false };
                    // Wire the check event
                    //checkbox.DidTapCheckBox -= habitDoneState_Click;
                    checkbox.DidTapCheckBox += habitDoneState_Click;
                    // Define properties
                    //checkbox.HeightAnchor.ConstraintEqualTo(24).Active = true;
                    //checkbox.WidthAnchor.ConstraintEqualTo(24).Active = true;

If you think this is a bug, I'll let you see what you can do.
Otherwise, feel free to close the issue.

@omatrot
Copy link
Author

@omatrot omatrot commented May 19, 2020

About that comment. I deleted it because it only worked once, on the next run I was not called anymore.

@mrbiggred
Copy link
Member

@mrbiggred mrbiggred commented May 19, 2020

@omatrot that explains why I saw it my email but not in the issue. Unless you have any objections I would like to leave your comment in the issue to help with debugging the issue.

Sorry I'm not able to reproduce your issue. Feel free to send me a reproducible example if you can. You can also check the BEMCheckBox issues and see if someone has reported a similar issue to the one your experiencing. I am planning on doing a new release but there are not many changes in BEMCheckBox since the [1.4.3] (Dec 22, 2017) release.

@omatrot
Copy link
Author

@omatrot omatrot commented May 25, 2020

Correct me if I'm wrong, but BEMCheckbox is now in Swift, but used to be Objective-C? I've modified the code in a fork to add a NSLog call if the delegate is not called.

I can't link my project with a modified version of yours, it fails with the following errors:

MTOUCH : error MT5211: Native linking failed, undefined Objective-C class: BEMCheckBox. The symbol '_OBJC_CLASS_$_BEMCheckBox' could not be found in any of the libraries or frameworks linked with your application.
MTOUCH : error MT5211: Native linking failed, undefined Objective-C class: BEMCheckBoxGroup. The symbol '_OBJC_CLASS_$_BEMCheckBoxGroup' could not be found in any of the libraries or frameworks linked with your application.

Do you have any idea what it takes to expose the Swift code to Objective-C?

@mrbiggred
Copy link
Member

@mrbiggred mrbiggred commented May 26, 2020

@omatrot you are correct that BEMCheckBox recently migrated to Swift. Version 1.4.3 was built using the objective-c version of BEMCheckBox. I think binding to Swift is similar to Objective-c based on this documentation.

How did you bind your forked BEMCheckbox to your project? Do you have an example project I can view to duplicate the problem?

Please note I'm currently migrating the build to GitHub Workflows but ran into and issue. Once I get the build sorted out I'll work on updating the XPlugin to consume the Swift version of BEMCheckbox.

@omatrot
Copy link
Author

@omatrot omatrot commented May 27, 2020

I forked this project and the BEMCheckBox one.
I simply built the iOS project, and replaced the framework library in the binding project of my fork.

You could easily reproduced the problem by playing with my fork of your project.

@omatrot
Copy link
Author

@omatrot omatrot commented Jun 6, 2020

Ok, I want to let you know that I solved my problem by simply subclassing BEMCheckBox.
The idea comes from this article.
Don't Ask me why it works now. I have no idea. I just think that somehow the native part of the BEMCheckBox lost the handle to the Managed delegate somewhere, may be because the checkbox are created on the fly and never referenced in memory for the managed part.

@omatrot omatrot closed this as completed Jun 6, 2020
@omatrot omatrot changed the title ClickEvent does not work DidTapCheckBox is not called Jun 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants