Can I do an "if" in OBJ-C

Hello, I’m working on the IP menulet thats been floating around. I have it displaying the IP address instead of an Icon. What I would like to do is, on initialize, check the info.plist and see what the bool is telling it to display. Either IP Address, or Icon. I have added a submenu to the menulet called “Display”, the sub menu choices are “IP Address”, and “IP Icon”. I would like to toggle the bit in the info.plist with the sub menu items to control what is being displayed. Would I use an “if” statement?, and are submenu’s indexed a b c. Thanks

//-Here is the IP Address display

@implementation IPMenulet
NSString * externalIP;
-(void)dealloc
{
[statusItem release];
[super dealloc];
}

  • (void)awakeFromNib
    {
    statusItem = [[[NSStatusBar systemStatusBar]
    statusItemWithLength:NSVariableStatusItemLength]
    retain];
    [statusItem setHighlightMode:YES];

    [statusItem setEnabled:YES];
    [statusItem setToolTip:@“IP Notifier”];

    [statusItem setMenu:theMenu];

    [self getExtenalIP];
    ipMenuItem = [[NSMenuItem alloc] initWithTitle:externalIP
    action:@selector(updateIPAddress:) keyEquivalent:@“”];

    [ipMenuItem setTarget:self];
    [theMenu insertItem:ipMenuItem atIndex:1];

    [statusItem setTitle:[NSString stringWithString:externalIP]];
    [NSTimer scheduledTimerWithTimeInterval:3600 target:self selector:@selector(updateIPAddress:) userInfo:nil repeats:YES];
    }

-(IBAction)updateIPAddress:(id)sender
{
[ipMenuItem setTitle:@“Updating…”];
[statusItem setTitle:@“Updating…”];
[self getExtenalIP];

[ipMenuItem setTitle:[NSString stringWithString:externalIP]];
[statusItem  setTitle:externalIP];

}

  • (void)getExtenalIP
    {

    externalIP=@“”;
    NSUInteger an_Integer;
    NSArray * ipItemsArray;
    ######################################################################################

//-Here is the Icon Display

[ipMenuItem setTarget:self];
[theMenu insertItem:ipMenuItem atIndex:1];

NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *path = [bundle pathForResource:@“ip” ofType:@“png”];
menuIcon= [[NSImage alloc] initWithContentsOfFile:path];

[statusItem setTitle:[NSString stringWithString:@“”]];
[statusItem setImage];
[menuIcon release];

Hi,

first of all, Info.plist is definitely the wrong place to store user data.
The designated place is user defaults.

I put everything (except the image) into a Xcode project
You can download it here

Hi StefanK, Thanks for putting that together. How long did it take you? It seems I get started trying to figure out how things work, and the next thing I know, 6 hours have past. There are two plists, one with bundle info, and the other for local info. I thought that is where I could store and pull info from, because there are a few more things I want to do. I tried moving the IP Address, and IP Icon buttons to a submenu in the same menu. I had everything linked I think, but I couldn’t get it to work. Is it because the instructions reference menu, and not menu (menu). Thanks again, adown

10 minutes to write the code, but I spent more than one year to learn ObjC deeply

Hello again, I’d like to say I have one more question, but that just wouldn’t be true. If I move the two display buttons into a sub menu, and use, setSubmenu:forItem:, does that mean I can’t also have a couple single buttons in the menu to do something else. Thanks, adown

Of course you can add more menu items, but make all the menu settings in Interface Builder.
Creating menus and menu items programmatically makes sense only if they are supposed to be dynamic.

An exception is the status menu item, it cannot be connected in interface Builder