Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
New Developer
Posts: 34
Registered: ‎03-23-2011
My Device: Bold 9700

Calling a method from another class

I have a situation that I can't wrap my head around and I need some help.

 

My main application file creates a references to 4 other class files that are different views/screens of my application.  In one of the views I need to call a method from another view but can't figure out how to do that without making another instance of the 2nd view in the 1st view.  Here's a basic example:

 

 

Test.as

 

package
{
	import flash.display.Sprite;
	
	import views.View1;
	import views.View2;

	[SWF(height="600", width="1024", frameRate="30")]
	public class Test extends Sprite
	{
		private var view1:View1;
		private var view2:View2;
		
		public function Test()
		{
			view2 = new View1();
			view2 = new View2();
		}
	}
}

 

View1.as

 

package views 
{ 
	import qnx.ui.text.Label;
	
	public class View1 extends Sprite
	{
		private var lblTest:Label;
		
		public function View1()
		{
			lblTest = new Label();
			lblTest.text = "test";
			lblTest.setPosition(10, 10);
		}

		private function updateLabel():void
		{
			lblTest.text = "updated";
		}
	}
}

 

 

View2.as

 

package views 
{ 
	import qnx.ui.text.Label;
	
	public class View2 extends Sprite
	{
		public function View2()
		{
			//call updateLabel on view1
		}
	}
}

 

So how does View2 call the method updateLabel() on View1 when both views are already instantiated in Test.as?

 

 

Developer
Developer
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook

Re: Calling a method from another class

Create a variable for View1 in your View2 class and pass a reference to it.

 

ie:

 

public class View1 {
  //CODE
}

public class View2 {
   private var _view1:View1;

   //CODE

   public function setView1(v1:View1):void {
      _view1 = v1;
   }
}

 

 

or you could pass the reference in the constructor like this

 

 

public class View1 {
  //CODE
}

public class View2 {
   private var _view1:View1;
   
   public View2(v1:View1 = null) {
      _view1 = v1;
   }

   //CODE
}

 

With the second option it defaults to null in case you don't always need the reference

 

Developer
Posts: 386
Registered: ‎12-12-2010
My Device: BB Style

Re: Calling a method from another class

Firstly, I am going to assume that you actully meant this in Test.as:

 

 

view1 = new View1();
view2 = new View2();

 

 

and not

 

 

view2 = new View1();
view2 = new View2();

 

 

==================

Secondly, The scope of the instantiation for view1, view2 instances are limited to the Test class.

If you have to invoke updateLabel() in view1, you have to invoke it in Test.as  or instantiate another view1 inside view2

New Developer
Posts: 34
Registered: ‎03-23-2011
My Device: Bold 9700

Re: Calling a method from another class

Ok so I tried those and didn't get the results I wanted. The next question is how do I call updateLabel on Test's instance of View1 from within View2?

 

Something like this:

 

 

package views 
{ 
	import qnx.ui.text.Label;
	
	public class View2 extends Sprite
	{
		public function View2()
		{
			Test.view1.updateLabel();
		}
	}
}

 

 

Regular Contributor
Posts: 75
Registered: ‎03-21-2011
My Device: None

Re: Calling a method from another class

[ Edited ]

Well first of all you can't as written because you've made it private. updateLabel() needs to be a public function for it to be accessed outside the class. Use CMY's second implementation to pass the variable in the constructor, and then access that variable, not the top-level class (which you have to work around to access anyway).

 

Your classes should look like this

 

Test.as

package
{
	import flash.display.Sprite;
	
	import views.View1;
	import views.View2;

	[SWF(height="600", width="1024", frameRate="30")]
	public class Test extends Sprite
	{
		private var view1:View1;
		private var view2:View2;
		
		public function Test()
		{
			view2 = new View1();
			view2 = new View2(view1); //note we pass view1
		}
	}
}

 

View2.as

public class View2 extends Sprite {
   
   private var _view1:View1;
   
   public function View2(v1:View1 = null) {
      _view1 = v1;
      updateView();
   }

   public function updateView():void {
      view1.updateLabel();
   }

   //other functions and such
   
}

 

View1.as

 

package views 
{ 
	import qnx.ui.text.Label;
	
	public class View1 extends Sprite
	{
		private var lblTest:Label;
		
		public function View1()
		{
			lblTest = new Label();
			lblTest.text = "test";
			lblTest.setPosition(10, 10);
		}

		public function updateLabel():void
		{
			lblTest.text = "updated";
		}
	}
}

 

The other thing you can do, though it's bad coding practice, is to pass a reference to Test.as to every subclass of Test, call it "parent" or something, and then use parent.subClass.function(var). But don't do that because like I said it's bad practice.

 

Developer
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10

Re: Calling a method from another class

An alternative method would be to dispatch an event to a common event dispatcher (like a singleton) to tell "others" that something has occurred (state has changed) or something needs to occur (update your state).  The above suggestions will work but makes for a "tightly coupled" system.  Event dispatching makes for a "loosely coupled" systems and allows for other views to tell another view to update/refresh/etc.  Passing object references all around makes for code that is hard to maintain and harder to reuse.

 

Just a suggestion.

 

Developer
Posts: 306
Registered: ‎03-01-2011
My Device: Playbook (sim)

Re: Calling a method from another class

another method is to create a global class, using static objects and methods.you store a static object inside a class and make a static method that handles the memory managment in the object. the method could have a getReference and a setReference option. this options are saving references to your actual objects. and because it's static you can access it from everywhere.

..................................................................................................................................................................................................................

My App: Peg Solitaire

Need a designer? Drop me a line
New Developer
Posts: 34
Registered: ‎03-23-2011
My Device: Bold 9700

Re: Calling a method from another class

Thanks everyone for the help.  I'm going to use macwarrior's  solution because it's the easiest for me to implement right off the bat and with only have a few more days to get my app in for the offer I'm in a hurry.  Also I'm new to actionscript, I come from other languages, and I'd have to figure out the other solutions.

 

Does anyone have any examples of the event dispatching and the global static class so I can learn up on it?

Developer
Posts: 54
Registered: ‎12-22-2010
My Device: Not Specified

Re: Calling a method from another class

I say go ahead and try to submit your app before the 31st as you can always make improvements later. In term of dispatching event, just google "dispatching custom events in actionscript 3" and you will get plenty of good tutorials (much better than any explanation I can post here).

 

Besides examples, if you're serious about developing using actionscript, I suggest you read up on the documentation and go through the EventDispatcher class as well as check out the display list so that you can better understand the relationship between parent and child display objects. 

 

For now just get you app rolling and submit it.

 

 

Developer
Posts: 409
Registered: ‎12-10-2010
My Device: PlayBook

Re: Calling a method from another class

dispatching and listening for events is the standard approach for communication within actionscript 3.0:  Introduction to event handling in ActionScript 3.0

 

usually events bubble within a stack, but in your case you've created two branches and you want to open up a communication channel between the two.

 

if each branched object has access to the stage, as any display object, you can use the stage as both the dispatcher and listener for your custom event.  for other objects that don't have access to the stage singleton, you can either create your own singleton to handle your custom events or you could even (perhaps eventually) create you own custom broadcaster class once you understand how events work.

 

 


PlayBook Applications:
Drop Swatch