Open View from Menu

Hello,
I would like to open a new view from a menu.

Here is part of my code but it does not work:

function onMenuItem(item) {
    if (item == :Choose) {
        Storage.setValue("actualTimer",  Storage.getValue("setupTimer"));
        Storage.setValue("count", 0);
    	WatchUi.pushView(new TimerView(), new TimerDelegate(), WatchUi.SLIDE_UP);
    } else if (item == :Change) {
        WatchUi.pushView(new ChangeView(), new ChangeDelegate(), WatchUi.SLIDE_UP);
    }
}

Can somebody help me with it?

  • This code works fine for me...

    using Toybox.WatchUi;
    using Toybox.Graphics;
    using Toybox.Application;
    
    class StringView extends WatchUi.View {
    
    
        function initialize() {
            View.initialize();
        }
    
        function onLayout(dc) {
        }
    
        function onUpdate(dc) {
            dc.setColor( Graphics.COLOR_BLACK, Graphics.COLOR_BLACK );
            dc.clear();
            dc.setColor( Graphics.COLOR_WHITE, Graphics.COLOR_TRANSPARENT );
    
            dc.drawText( dc.getWidth() / 2, dc.getHeight() * 2 / 5, Graphics.FONT_LARGE, "ABCD", Graphics.TEXT_JUSTIFY_CENTER );
            dc.drawText( dc.getWidth() / 2, dc.getHeight() * 3 / 5, Graphics.FONT_NUMBER_MEDIUM, "1234", Graphics.TEXT_JUSTIFY_CENTER );
        }
    }
    
    class StringDelegate extends WatchUi.BehaviorDelegate {
    
        function initialize() {
            BehaviorDelegate.initialize();
        }
    
        function onMenu() {
            WatchUi.pushView(new StringMenu(), new StringMenuDelegate(), WatchUi.SLIDE_UP);
            return true;
        }
    }
    
    class StringMenu extends WatchUi.Menu {
    
        function initialize() {
            Menu.initialize();
    
            Menu.addItem("Timer", :Choose);
            Menu.addItem("Change", :Change);
        }
    }
    
    class StringMenuDelegate extends WatchUi.MenuInputDelegate {
    
        function initialize() {
            MenuInputDelegate.initialize();
        }
    
        function onMenuItem(item) {
            if (item == :Choose) {
                WatchUi.pushView(new TimerView(), new TimerDelegate(), WatchUi.SLIDE_UP);
            } else if (item == :Change) {
                WatchUi.pushView(new ChangeView(), new ChangeDelegate(), WatchUi.SLIDE_UP);
            }
    
            //return true;
        }
    }
    
    class TimerView extends WatchUi.View {
    
        function initialize() {
            View.initialize();
        }
    
        function onUpdate(dc) {
            dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_WHITE);
            dc.clear();
    
            var cx = dc.getWidth() / 2;
            var cy = dc.getHeight() / 2;
    
            dc.drawText(cx, cy, Graphics.FONT_SMALL, "Timer View", Graphics.TEXT_JUSTIFY_CENTER);
        }
    }
    
    class TimerDelegate extends WatchUi.BehaviorDelegate {
    
        function initialize() {
            BehaviorDelegate.initialize();
        }
    }
    
    class ChangeView extends WatchUi.View {
    
        function initialize() {
            View.initialize();
        }
    
        function onUpdate(dc) {
            dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_WHITE);
            dc.clear();
    
            var cx = dc.getWidth() / 2;
            var cy = dc.getHeight() / 2;
    
            dc.drawText(cx, cy, Graphics.FONT_SMALL, "Change View", Graphics.TEXT_JUSTIFY_CENTER);
        }
    }
    
    class ChangeDelegate extends WatchUi.BehaviorDelegate {
    
        function initialize() {
            BehaviorDelegate.initialize();
        }
    }
    
    class StringApp extends Application.AppBase {
    
        function initialize() {
            AppBase.initialize();
        }
    
        function getInitialView() {
            return [ new StringView(), new StringDelegate() ];
        }
    }
    

  • Thanks for your answer. I found a copy-paste misstake in my code, sorry... Now it works...