Giter VIP home page Giter VIP logo

statusbarapp's Introduction

Create a StatusBar App

  • category: work
  • date: 2012-07-20 21:15
  • tags: learning, cocoa

It has been months since I decided to learn cocoa development, but in vain. Unlike python, the documentation of cocoa tortures me a lot.

I am still new to cocoa development. And this post will be part of my learning series. I didn't mean to teach you anything, on the contrary, it is a track of my learing. But it may be a little help to you.

StatusBar App

A StatusBar App is what on the right side of the menu bar, it doesn't contain a main window. For example, the volumn control is a StatusBar App.

Demo Time

It's the time to create a StatusBar App now.

  1. Open your Xcode (I am on Xcode 4.3.3)

  2. Create a cocoa application project

    create a project

  3. Name the project StatusBarApp

  4. Run for testing

Now you will get an App with window. However, our app is a StatusBar App, it has no window.

The final app should be like:

application

Menu

We will create the menu first.

  1. Drag a menu to the interface builder

    drag a menu

  2. Edit the menu item

    edit menu item

  3. Decorate the menu with seprator

    decorate with seprator

  4. Connect the menu to your code outlet

    connect menu outlet

StatusBar

Create the statusBar property in your AppDelegate.h file:

@property (strong, nonatomic) NSStatusItem *statusBar;

Synthesize it in the AppDelegate.m file:

@synthesize statusBar = _statusBar;

Initialize the statusBar:

- (void) awakeFromNib {
    self.statusBar = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];

    self.statusBar.title = @"G";

    // you can also set an image
    //self.statusBar.image =

    self.statusBar.menu = self.statusMenu;
    self.statusBar.highlightMode = YES;
}

awakeFromNib is earlier than applicationDidFinishLaunching in the lifecycle.

App Go

Let's test this application.

  1. Run you app now. You will see a G in the menu bar.
  2. Delete the useless window in your MainMenu.xib, and run your app again.

It works! But it doesn't work the right way. It is on the dock, it shows the menu on the left.

Fix it in StatusBarApp-Info.plist, add a row:

Application is agent (UIElement)  = YES

connect menu outlet

Run your application again, it won't be on the dock, it won't show the menu.

Do More

But you can't quit the application, that could be annoying. We did have a Quit item on the application, but it won't work right now.

Fix it:

quit

Source Code

Get the latest source code at GitHub.

Follow me on GitHub.

Reference:

statusbarapp's People

Watchers

Fabian Morón Zirfas avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.