필터 지우기
필터 지우기

Notify event in a class

조회 수: 6 (최근 30일)
Mateus Banroc
Mateus Banroc 2019년 12월 27일
댓글: Mateus Banroc 2019년 12월 27일
Hi guys,
I'm having some problems with event in classes. I have a class class like this:
classdef ClassA < handle
properties
prop
end
events
EventA
end
methods
function o = ClassA
addlistener(o,'EventA',@o.callbackfun);
o.prop = ClassB;
end
function callbackfun(o,~,~)
% do something
end
end
end
and the ClassB like:
classdef ClassB < handle
properties
button
end
methods
function o = ClassB
fig = figure;
o.button = uicontrol(fig,'Style','pushbutton','Callback',@(~,~,~) notify(ClassA,'EventA'));
end
end
end
And then, when I click the button, it opens a new figure. Like it was calling ClassA again. What I am doing wrong?
Thanks,

채택된 답변

Max Murphy
Max Murphy 2019년 12월 27일
You are very close to having it working! In your Notify callback, you make a call to the constructor ClassA rather than a specifieid object of type ClassA. So you would give the argument to the ClassB constructor to pass an object of type ClassA and then reference that object instead of the class constructor:
classdef ClassB < handle
properties
button
end
methods
function o = ClassB(classAObj)
fig = figure;
o.button = uicontrol(fig,'Style','pushbutton','Callback',@(~,~,~) notify(classAObj,'EventA'));
end
end
end
  댓글 수: 1
Mateus Banroc
Mateus Banroc 2019년 12월 27일
It works!! Thanks Max!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by