NSAlert on window load

Discussion in 'Mac Programming' started by faffoo, Jul 8, 2011.

  1. faffoo macrumors 6502

    Joined:
    May 22, 2008
    Location:
    Glasgow, UK
    #1
    Hi there,

    I have an app delegate, and a NSWindowController that I am initialising and allocating with a set up XIB. My question:

    I would like to show an NSAlert as a sheet on this new window, as soon as the window is shown. How can i accomplish this?

    Regards
    Matt
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    Override windowDidLoad in your NSWindowController and show the alert sheet from there.
     
  3. faffoo thread starter macrumors 6502

    Joined:
    May 22, 2008
    Location:
    Glasgow, UK
    #3
    Hi there,

    When i do that, it shows, but not in a sheet layout. Also, any interface elements I add to the alert that I have set up in a XIB will not be added because at that point for some reason they are still nil.

    Any reason why I am not getting the slick animation and the alert showing as a sheet?

    All help appreciated
    Matt
     
  4. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    I can only guess. In my SimpleBrowser app, I show an alert if there's no internet connectivity using this technique. Although, it's literally an NSAlert, not a custom sheet. Can you post the relevant code? Particularly when and how you load the sheet.
     
  5. faffoo, Jul 9, 2011
    Last edited: Jul 9, 2011

    faffoo thread starter macrumors 6502

    Joined:
    May 22, 2008
    Location:
    Glasgow, UK
    #5
    Code:
    NSProgressIndicator *progressIndic = [[NSProgressIndicator alloc] initWithFrame:NSRectFromCGRect(CGRectMake(0, 0, 400, 20))];
        [progressIndic setStyle:NSProgressIndicatorBarStyle];
        [progressIndic startAnimation:nil];
        downloading = [NSAlert alertWithMessageText:@"Getting details" defaultButton:@"Use" alternateButton:nil otherButton:nil informativeTextWithFormat:@"%@", @"We are now attempting to contact our servers, and get your current licensing information. Please wait a moment or two."];
        [downloading setAccessoryView:progressIndic];
        NSButton *button = [[downloading buttons] objectAtIndex:0];
        [button setHidden:YES];
        [downloading beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:nil];
        [progressIndic release];
    shows the alert view but as a modal, not as the desired sheet i would like.

    I removed references to anything in XIB files here as they were all returning nil at this point. But i still need to be able to access controls in the xib file, any ideas when i can do this?

    Any ideas?
    Regards
    Matt
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    You could try showing the alert in the showWindow: method (assuming it's being used):

    Code:
    - (IBAction)showWindow:(id)sender {
        [super showWindow:sender];
    
        // show your NSAlert here
    }
    Or use windowDidBecomeKey: and show it there, if it's not already visible.
     
  7. faffoo thread starter macrumors 6502

    Joined:
    May 22, 2008
    Location:
    Glasgow, UK
    #7
    I found the problem, which may come in helpful for others searching:

    In my XIB file i had added a general object and set its class to my controller and connected my controls to the outlets through that control.

    I changed that so that the files owner had its class set to my controller and linked things up through that. Now, no problems.

    Thanks for your help everyone!

    Regards
    Matt
     

Share This Page