I have a ViewController class called GamePlay. In GamePlay there is a nested class called MyPinAnnotationView. When MyPinAnnotation's method TouchesBegan() gets called, I want to call a method CheckAnswer() from the parent GamePlay.
I do not want to create a new GamePlay instance because I have variables and instances already set. Can I access the parent in some way? ( Other than event listeners)
The nested class will only be able to reference static members in the parent. If you want to access instance members, you need to get a reference to the instance. The simplest way to do this is to add it as a parameter to the constructor of MyPinAnnotationView like so:
class MyPinAnnotationView
{
private GamePlay gamePlay;
public MyPinAnnotationView(GamePlay gamePlay)
{
this.gamePlay = gamePlay;
}
public void TouchesBegan()
{
this.gamePlay.CheckAnswer();
}
}
When you instantiate MyPinAnnotationView from GamePlay, just do this:
MyPinAnnotation annotation = new MyPinAnnotation(this);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With