Please help to newbie, basics

Hello.

I want to create simple app, that works in background, recieving BLE packets, showing information from sensors.
But i stack at beginning.

I cant draw any text dynamically.

At first I create testApp and label in testView.

Added some code to testView:

    // Update the view
    function onUpdate(dc as Dc) as Void 
    {
        // Call the parent onUpdate function to redraw the layout
        View.onUpdate(dc);

        //View.findDrawableById("Background").setColor(getBackgroundColor());
        var _label = View.findDrawableById("label")  as Text;
        _label.setText(init_text);
    }

It worked.

After that i tried to change data from testMenuDelegate (where i will parse my BLE data in future).

And get error "Cannot find symbol ':findDrawableById' on class definition '$.Toybox.WatchUi.View'.". 

Also i tried to add public function:

public function update_label(buf as Toybox.WatchUi.Text) as Void

to class in testView  and after that i tried:

testView.update_label("Hello world");
from testMenuDelegate (onMenuItem).
And get error :  "Cannot find symbol ':update_label' on class definition '$.testView'"
Why this happens ? How can i change the drawings correctly from other modules of program? 
  • It's hard to say for sure without seeing more code, but it sounds like you're trying to update_label on the class itself as opposed to an instance of the class, as per the error message: Cannot find symbol ':update_label' on class definition '$.testView'

    One of main concepts of object-oriented programming is that a class is distinct from an instance of the same class: stackoverflow.com/.../the-difference-between-classes-objects-and-instances

    The top answer describes it pretty well: stackoverflow.com/.../1486212

    A class is a blueprint which you use to create objects. An object is an instance of a class - it's a concrete "thing" that you made using a specific class

    This is easy to understand if you look at an example. For example, suppose you have a class House. Your own house is an object and is an instance of class House. Your sister's house is another object

    Note: to distinguish classes from objects, it's usually customary to capitalize class names.

    So in your case, you've defined update_label which requires an instance of the testView class, but you're apparently trying to call it on the class itself. This doesn't work because there could be theoretically be zero or many instances of testView in existence, and the program wouldn't know which one to call update_label on.

    The solution here is to make your testView class instance/object available to your testMenuDelegate, so it can call update_label on an instance of testView. You can do this by changing the definition of your testMenuDelegate initializer so it accepts a testView object as an argument.

    The code that currently creates a new testMenuDelegate should be changed to pass in a reference to the view instance.

    e.g. https://pastebin.com/Xq381HJc

    The class it's part of may need to be changed in a similar way, so that it receives a reference to the view instance.

    If you need help with this, please post more code, such as the code which creates a new testMenuDelegate, and any relevant code leading back to testView, and I can try to show you what changes to make.

  • For example, if testView is your top level view (returned by getInitialView), and you have an input delegate for that which opens a menu with testMenuDelegate:

    https://pastebin.com/0b8cRs6r

    I can’t edit my previous comment but the first paragraph should’ve read:

    It's hard to say for sure without seeing more code, but it sounds like you're trying to call update_label

  • Thanks a lot! Now i understand!