Manejo de applicationDidBecomeActive-"¿Cómo puede un controlador de vista responder a la aplicación que se activa?"
Tengo el protocolo UIApplicationDelegate
en mi AppDelegate principal.clase m, con el método applicationDidBecomeActive
definido.
Quiero llamar a un método cuando la aplicación regresa desde el fondo, pero el método está en otro controlador de vista. ¿Cómo puedo comprobar qué controlador de vista se muestra actualmente en el método applicationDidBecomeActive
y luego hacer una llamada a un método dentro de ese controlador?
4 answers
Cualquier clase en su aplicación puede convertirse en un "observador" para diferentes notificaciones en la aplicación. Cuando cree (o cargue) su controlador de vista, querrá registrarlo como observador para UIApplicationDidBecomeActiveNotification
y especificar qué método desea llamar cuando se envíe esa notificación a su aplicación.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(someMethod:)
name:UIApplicationDidBecomeActiveNotification object:nil];
No te olvides de limpiar después de ti mismo! Recuerde quitarse a sí mismo como el observador cuando su punto de vista se va:
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidBecomeActiveNotification
object:nil];
Más información sobre el Centro de notificación .
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-15 06:54:31
Swift 3, 4 Equivalente:
Añadiendo observador
NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidBecomeActive),
name: .UIApplicationDidBecomeActive,
object: nil)
Removing observer
NotificationCenter.default.removeObserver(self,
name: .UIApplicationDidBecomeActive,
object: nil)
Devolución de llamada
@objc func applicationDidBecomeActive() {
// handle event
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-20 15:38:18
Swift 2 Equivalente:
let notificationCenter = NSNotificationCenter.defaultCenter()
// Add observer:
notificationCenter.addObserver(self,
selector:Selector("applicationWillResignActiveNotification"),
name:UIApplicationWillResignActiveNotification,
object:nil)
// Remove observer:
notificationCenter.removeObserver(self,
name:UIApplicationWillResignActiveNotification,
object:nil)
// Remove all observer for all notifications:
notificationCenter.removeObserver(self)
// Callback:
func applicationWillResignActiveNotification() {
// Handle application will resign notification event.
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-16 18:59:49
Con Swift 4, Apple advierte a través de un nuevo compilador que evitemos el uso de #selector
en este escenario. La siguiente es una manera mucho más segura de lograr esto:
Primero, cree un var perezoso que pueda ser utilizado por la notificación:
lazy var didBecomeActive: (Notification) -> Void = { [weak self] _ in
// Do stuff
}
Si necesita que se incluya la notificación real, simplemente reemplace _
por notification
.
A continuación, configuramos la notificación para observar si la aplicación se activa.
func setupObserver() {
_ = NotificationCenter.default.addObserver(forName: .UIApplicationDidBecomeActive,
object: nil,
queue:.main,
using: didBecomeActive)
}
El gran cambio aquí es que en lugar de llamar a un #selector
, ahora llamamos al var creado anteriormente. Esto puede eliminar situaciones en las que se produce un bloqueo del selector no válido.
Finalmente, eliminamos al observador.
func removeObserver() {
NotificationCenter.default.removeObserver(self, name: .UIApplicationDidBecomeActive, object: nil)
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-25 12:26:41