主要内容

update

类: matlab.ui.componentcontainer.ComponentContainer
命名空间: matlab.ui.componentcontainer

设置属性后更新 UI 组件容器子类的实例

语法

update(obj)

说明

update(obj) 在一个或多个属性值发生更改后更新 UI 组件的内容。使用新属性值定义此方法以更新 UI 组件中的底层图形对象。此方法在下一次 drawnow 执行期间用户更改 UI 组件的一个或多个属性值之后执行。

输入参数

全部展开

matlab.ui.componentcontainer.ComponentContainer 基类继承的类的对象。

属性

Abstracttrue
Protectedtrue

要了解方法的属性,请参阅方法属性

示例

全部展开

定义名为 IPAddressComponent 的类,该类创建一个自定义组件,用于输入四个值来构成 IP 地址。

要定义该类,请创建包含以下类定义的名为 IPAddressComponent.m 的文件,该文件具有如下特性:

  • 存储 IP 地址的 Value 公共属性。

  • NumericFieldGridLayout 私有属性,它们在一个水平行中放置四个数值编辑字段。

  • 一个 setup 方法,用于初始化 NumericFieldGridLayout

  • 一个 update 方法,用于在 IP 地址发生变化时更新 NumericField 值。

  • 一个 handleNewValue 方法,它基于 4 个数值编辑字段的值设置 Value 属性。

classdef IPAddressComponent < matlab.ui.componentcontainer.ComponentContainer
    % IPAddressComponent a set of 4 edit fields for IP Address input
    properties
        Value (1,4) {mustBeNonnegative, mustBeInteger, mustBeLessThanOrEqual(Value, 255)} = [192 168 1 2];
    end
    
    events (HasCallbackProperty, NotifyAccess = protected)
        ValueChanged % ValueChangedFcn callback property will be generated
    end

    
    properties (Access = private, Transient, NonCopyable)
        NumericField (1,4) matlab.ui.control.NumericEditField
        GridLayout matlab.ui.container.GridLayout
    end
    
    methods (Access=protected)
        function setup(obj)
            % Set the initial position of this component
            obj.Position = [100 100 150 22];
            
            % Layout
            obj.GridLayout = uigridlayout(obj,[1,4], ...
                'RowHeight',{22},'ColumnWidth',{'1x','1x','1x','1x'},...
                'Padding',0,'ColumnSpacing',2);
            
            % Building blocks
            for k = 1:4
                obj.NumericField(k) = uieditfield(obj.GridLayout, 'numeric',...
                    'Limits', [0 255], 'RoundFractionalValues', true, ...
                    'FontName', 'Courier New', 'FontWeight', 'bold', ...
                    'ValueChangedFcn',@(src,event) obj.handleNewValue());
            end
          
        end
        
        function update(obj)
            % Update view
            for k = 1:4
                obj.NumericField(k).Value = obj.Value(k);
            end
        end
    end
       
    methods (Access=private)
        function handleNewValue(obj)
            obj.Value = [obj.NumericField.Value];  
            
            % Execute the event listeners and the ValueChangedFcn callback property
            notify(obj,'ValueChanged');
        end
    end
end

接下来,通过调用由 ComponentContainer 类提供的 IPAddressComponent 构造函数方法来创建组件,并将对象返回为 h。指定一个函数,当组件值更改时,该函数在命令行窗口中显示新 IP 地址。

 h = IPAddressComponent;

Figure contains an object of type ipaddresscomponent.

 h.ValueChangedFcn = @(src,event) disp("Value changed to: " + num2str(h.Value));

在编辑字段中输入 IP 地址 192.168.1.10。MATLAB® 在命令行窗口中显示更新后的 IP 地址。

提示

  • 不要在 UI 组件类的 setupupdate 方法中调用 drawnow。此类调用可能导致使用 UI 组件的 App 出现意外的屏幕更新。推荐的做法是,让 App 创建者(即您的组件用户)在需要触发屏幕更新时在其 App 代码中调用 drawnow。这些来自组件代码外部的调用会更新 App 中的所有 UI 组件,包括那些使用 ComponentContainer 类创建的组件。

版本历史记录

在 R2020b 中推出