MagicListener

MagicListener object
450 download
Aggiornato 16 gen 2012

Visualizza la licenza

MagicListener constructor creates a listener just like the addlistener function. The difference is the constructor accepts one more input, one object or a cell of objects, and the life cycle of the returned object will be linked also on these objects.

Exemple:

Without MagicListener:

[Class1.m]
classdef Class1 < handle
events
myEvent
end
end

[Class2.m]
classdef Class2 < handle
methods
function this = Class2( obj1 )
addlistener( obj1, 'myEvent', @(H,E) this.callback() );
end
function callback( this )
disp( 'CALLBACK!' );
end
end
end

>> obj1 = Class1();
>> obj2 = Class2( obj1 );
>> notify( obj1, 'myEvent' );
CALLBACK!
>> delete( obj2 );
>> notify( obj1, 'myEvent' );
Warning: Error occurred while executing callback:
Invalid or deleted object.

With MagicListener:

[Class1.m]
classdef Class1 < handle
events
myEvent
end
end

[Class2.m]
classdef Class2 < handle
methods
function this = Class2( obj1 )
MagicListener( obj1, 'myEvent', @(H,E) this.callback(), this );
end
function callback( this )
disp( 'CALLBACK!' );
end
end
end

>> obj1 = Class1();
>> obj2 = Class2( obj1 );
>> notify( obj1, 'myEvent' );
CALLBACK!
>> delete( obj2 );
>> notify( obj1, 'myEvent' );

Cita come

Benoit Charles (2024). MagicListener (https://www.mathworks.com/matlabcentral/fileexchange/34606-magiclistener), MATLAB Central File Exchange. Recuperato .

Compatibilità della release di MATLAB
Creato con R2008b
Compatibile con qualsiasi release
Compatibilità della piattaforma
Windows macOS Linux
Categorie
Scopri di più su Handle Classes in Help Center e MATLAB Answers
Tag Aggiungi tag
Riconoscimenti

Ispirato: Graphical Wrappers

Community Treasure Hunt

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

Start Hunting!
Versione Pubblicato Note della release
1.0.0.0