Translate

domingo, 5 de febrero de 2012

XCode: Mensajes de aviso / Alert Messages

El manejo de mensajes de aviso se puede hacer a través de los objetos NSAlert, los cuales nos permiten crear mensajes de avisos con diferentes botones y que a su vez estos botones puedan realizar una acción determinada.
En el ejemplo de hoy vamos a hacer un botón que cierre nuestra aplicación. Una vez pulsado el botón se mostrará un mensaje solicitando la confirmación de cerrar o no la aplicación.

This time we are going to see how to display alert messages in our application. For this purpose we have the class NSAlert. With this class we could create alert messages using buttons, message text, icons as we need.
The example application we are going to create shows a button in the centre of the windows, once this button is pressed a message appear asking you if you really want to close the application.




Crearemos un proyecto vacío (ponle el nombre que te de la gana, es lo de menos). No hace falta Core Data ni nada mas, será un ejemplo muy simple.
Colocaremos un botón en el centro de la ventana principal y le pondremos de título "Cerrar". Después crearemos un Action asociado a este botón sobre el fichero AppDelegate.h. Puedes llamar a esa Action como "cerrarAplicacion"


Start creating an empty project, (you can name as you want, it does´t matter). Also we do not need Core Data, or anything more, it´s going to be a very simple application.
Once you have created the empty application go to your main window and place a button in the center of it. Then create an Action from this bottom to the AppDelegate.h. "cerrarAplicacion" could by the name for the action. 

La implementación de dicho botón consistirá primero en crear un objeto del tipo NSAlert, después lo configuraremos con los diferentes botones, y textos que necesitemos mostrar. Y por último haremos una llamada a mostrar el mensaje de alerta creado, indicando sobre que ventana mostrado, y a que método llamar cuando se pulse alguno de los botones.
El método se llamará "alertDidEnd", y en el implementaremos lo que necesitamos que se haga al pulsar el botón correspondiente. En nuestro caso será cerrar el programa, lo que se hará simplemente usando la función exit(0).

The implementation for this action will consist first on create an NSAlert object, and then populate this object with a pair of buttons, "Si, sierra" and "No en ningún caso", the texts wich explain the alert, and also the type of alert. Finally we show the alert, setting from which window appears (_window), the behavior of the alert, in this case, modal behavior, and the selector called when the user press one of the buttons in the alert message.
This selector calls a method called "alertDidEnd". It´s implements the action to close the application in  case the user press the first button defined. Using exit(0) you can close the application.




No hay comentarios: