4

假设我有一个创建一个uifigure(见下文)的 GUI 类。我可以在我的工作区中创建这个类的一个实例:

tG = testGUI('hi!');

我可以通过调用方法关闭 GUI deletetG.delete(). tG当句柄从工作区中清除时,是否也可以自动关闭 GUI ,例如

clear tG

这将防止在多次运行某些脚本时打开该类的许多实例,而 GUI 的句柄已被删除。

更新

  • 首先删除registerApp(app, app.UIFigure)构造函数中的调用似乎解决了这个问题,但是这在我的真实 GUI 中不起作用。
  • 在测试 GUI 中向按钮添加回调会在我的 MVCE 中重现该问题。

classdef testGUI < matlab.apps.AppBase

    properties (Access = public)
        % The figure handle used.
        UIFigure
        % app name
        name
    end

    properties (Access = private)
        pushButton
    end

    methods (Access = public)

        function app = testGUI(name)
            %TESTGUI - Constructor for the testGUI class.

            % property management
            app.name = name;

            % create GUI components
            createComponents(app)

            % Register the app with App Designer
            %registerApp(app, app.UIFigure); % removing this does not solve the issue
        end

        function delete(app)
            delete(app.UIFigure)
        end
    end

    methods (Access = private)

        function createComponents(app)
            %Create the components of the GUI

            app.UIFigure = uifigure('Name', app.name);
            app.UIFigure.Visible = 'on';

            % some button
            app.pushButton = uibutton(app.UIFigure, 'push');
            app.pushButton.Text = 'This is a button';
            app.pushButton.ButtonPushedFcn = @(src,event) someCallBack(app);
        end

        function someCallBack(app)
            fprintf('this is someCallBack\n')
        end
    end
end
4

0 回答 0