Controller
open class Controller : IController
A Singleton IController
implementation.
In PureMVC, the Controller
class follows the
‘Command and Controller’ strategy, and assumes these
responsibilities:
- Remembering which
ICommand
s are intended to handle whichINotifications
. - Registering itself as an
IObserver
with theView
for eachINotification
that it has anICommand
mapping for. - Creating a new instance of the proper
ICommand
to handle a givenINotification
when notified by theView
. - Calling the
ICommand
‘sexecute
method, passing in theINotification
.
Your application must register ICommands
with the
Controller.
The simplest way is to subclass Facade
,
and use its initializeController
method to add your
registrations.
@see org.puremvc.swift.core.view.View View
@see org.puremvc.swift.patterns.observer.Observer Observer
@see org.puremvc.swift.patterns.observer.Notification Notification
@see org.puremvc.swift.patterns.command.SimpleCommand SimpleCommand
@see org.puremvc.swift.patterns.command.MacroCommand MacroCommand
-
Constructor.
This
IController
implementation is a Singleton, so you should not call the constructor directly, but instead call the static Singleton Factory methodController.getInstance()
@throws Error if Singleton instance has already been constructed
Declaration
Swift
public init()
-
Initialize the Singleton
Controller
instance.Called automatically by the constructor.
Note that if you are using a subclass of
View
in your application, you should also subclassController
and override theinitializeController
method in the following way:// ensure that the Controller is talking to my IView implementation public func initializeController() { view = MyView.getInstance { MyView() } }
Declaration
Swift
open func initializeController()
-
Controller
Singleton Factory method.Declaration
Swift
open class func getInstance(_ factory: () -> IController) -> IController
Parameters
factory
reference that returns
IController
Return Value
the Singleton instance of
Controller
-
Register a particular
ICommand
class as the handler for a particularINotification
.If an
ICommand
has already been registered to handleINotification
s with this name, it is no longer used, the newICommand
is used instead.The Observer for the new ICommand is only created if this the first time an ICommand has been regisered for this Notification name.
Declaration
Swift
open func registerCommand(_ notificationName: String, factory: @escaping () -> ICommand)
Parameters
notificationName
the name of the
INotification
factory
reference that instantiates and returns
ICommand
-
If an
ICommand
has previously been registered to handle a the givenINotification
, then it is executed.Declaration
Swift
open func executeCommand(_ notification: INotification)
Parameters
note
-
Check if a Command is registered for a given Notification
Declaration
Swift
open func hasCommand(_ notificationName: String) -> Bool
Parameters
notificationName
Return Value
whether a Command is currently registered for the given
notificationName
. -
Remove a previously registered
ICommand
toINotification
mapping.Declaration
Swift
open func removeCommand(_ notificationName: String)
Parameters
notificationName
the name of the
INotification
to remove theICommand
mapping for