itemClicked event selector objective c

| | August 11, 2015

I am extremely new to ObjC and am trying to build my first app. However I seem to have hit a barrier. I am building a menubar app and I want to create an alert when the item is clicked, to say that the click is acknowledged.

This is what I have written in the .h file, but I get an error asking me to end the statement after the (id)sender with a ;.

- (void)itemClicked:(id)sender {
    NSAlert * alert = [NSAlert alertWithMessageText:@"Toggle Acknowledge"
                                      defaultButton:@"Gotcha!"
                                    alternateButton:nil
                                        otherButton:nil
                          informativeTextWithFormat:@"NSStatusItem was clicked"]
    [alert runModal] }

Not sure what am doing here! Any help would be fantastic, thank you.

2 Responses to “itemClicked event selector objective c”

  1. Just do what the error asks, in Objective-C all lines must end with a semicolon

    - (void)itemClicked:(id)sender {
        NSAlert * alert = [NSAlert alertWithMessageText:@"Toggle Acknowledge"
                                          defaultButton:@"Gotcha!"
                                        alternateButton:nil
                                            otherButton:nil
                              informativeTextWithFormat:@"NSStatusItem was clicked", nil];
        [alert runModal];
    }
    

    I added a nil statement as the format parameter to avoid a warning.

    Edit:

    In Yosemite (and higher) it’s recommended to use this

    - (void)itemClicked:(id)sender {
        NSAlert * alert = [[NSAlert alloc] init];
        alert.messageText = @"Toggle Acknowledge";
        alert.informativeText = @"NSStatusItem was clicked";
        [alert addButtonWithTitle:@"Gotcha!"];
        [alert runModal];
    }
    
  2. Rohit suvagiya on November 30, -0001 @ 12:00 AM
    - (void)itemClicked:(id)sender
    {
         NSAlert * alert = [NSAlert alertWithMessageText:
                                          defaultButton:@"Gotcha!"
                                        alternateButton:nil
                                            otherButton:nil
                              informativeTextWithFormat:@"NSStatusItem was clicked", nil];
        [alert beginSheetModalForWindow:window modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:nil];
    }
    

    Just use and check then give me feedback

Leave a Reply