We practice (a) is completed in the status bar icon click, pop-up view effect, but also some seem to experience is not very consistent with the habits of users (for example, click on the application of other places, Popover does not automatically disappear) we continue to add and improve the function of some users, making the application looks good a little experience, then we add the mouse event for Popover and exit the application function.
The system in response to mouse events, we need to use the Cocoa framework in the NSEvent class to add a listener object for the application and development in the Mac, we usually are (mouse or keyboard) event driven OSX system, when an event is received, will be added to the queue window service process, then through the service process of asynchronous distributed events (events may be copy) to the application, the application will respond to the corresponding event by delegate.
The application in response to the incident, will generate a monitor (listen), usually we finished listening to events, do not have to manually release the object, API system will be release at the right time.
1.1 select the ViewController.m file, add the left mouse button to click on the event listener, the realization of the code as shown below:
Run the application, click the status bar icon shows the Popover, other places use the mouse to click on the application window, then you will find that Popover will automatically hide, this effect compared with the previous, more suitable to operate some. In addition, we continue to add a Popover to the button, to achieve the function to exit the application.
1.2 select the SBPopViewController.xib file, add button, and set the following figure:
1.3 selected SBPopViewController.m, button to add the event response code as shown below:
add button response code
Run the application again, if there is no error, click the status bar icon, the interface should look like the following figure:
Click the exit button, and you will find that the application will end the process and exit. Now our application can display Popover, and click on the application outside of Popover can be hidden, and you can manually exit the application, these basic functions have been completed, but from the beginning of the project to now, window has been accompanied by the start of each display window (it looks not what use in our application, we only design for) a demo application system in the status bar, window is not, so we have to deal with the window.
1.4 select the Main.storyboard file, remove from the project, and empty the project configuration, the following figure:
If you run the project, will find that window does not show (as has been deleted.), but did not show the status bar icon, application run up, but we need no desktop effects, when we open the activity monitor, found with is the normal operation of the (below).
activity monitor shows demo running properly
This is because the Xib file is loaded before we use the window system, for our default to do some other things, such as before the load window, create the application object, set the application proxy configuration window settings, the system is where do these things? It is in the main.m file system calls NSApplicationMain () this important function, the function of shielding a lot of details, the new entry is a good thing, can easily carry out the business code work without relationship system startup details; but if we need to study the system behavior in some start when it brings difficulty to us.
As in iOS, the application start loading is completed, the settings of UI -applicationDidFinishLaunching from the AppDelegate method: first, if we in the above project on the method of setting debugging breakpoints you will find application, after the start, did not call this method, namely we delete the main.storyboard file, the system is no longer we set automatically for the application of the delegate, we need to manually set in main.m.
1.5 select the main.m file, the code into the following figure:
main.m file implementation code
1.6 because we have removed the window, so the ViewController has no value for us, we just need to write in the ViewController before the code is placed into the AppDelegate.m to realize it, so we can delete the ViewController class. The project should look like the following figure:
If you’re in the middle of this step without error, then run the project again, you will find the icon on the status bar appeared again, and before we realize the function of the normal response can be ^_^
The whole project code, you can download StatusBarDemoApp from here, well, we finished this exercise is for the new project, to use Mac OSX development of the students to do some basic help slightly, due to the limited capacity of the author, if there is wrong in this exercise project, also please reply me at any time, or told in the comments, so that you can let other people see, common progress.
The project is just a small practice to start learning, you can add some functions of this exercise: for example, the menu bar, such as setting startup, or after running the hidden icon on the Dock, the code structure and reasonable organization, such as the method of event monitoring, packaged as a tool or, use tableview instead of SBPopViewController, add a network request data, so you can from this exercise gradually became a small Mac application, I wish you can harvest ~.