1

I have a view in the xib file, I want to put that view into a window.... I'm using this code:

- (void) popupNotificationWithTag:(NSString *)tag fade:(double)msFade lineOne:(NSString *)lineOneText lineTwo:(NSString *)lineTwoText
{
    NotificationWindow *notificationWindow;
    NotificationWindow *tmpWindow;
    NSEnumerator *enumerator;

    // Walk the notification windows in the array
    enumerator = [self.notificationWindows objectEnumerator];
    if(enumerator)
    {
        while((tmpWindow = [enumerator nextObject]))
        {
            if([tmpWindow.tag isEqualToString:tag])
            {
                notificationWindow = tmpWindow;
            }
        }
    }

    // Make a new notification window
    if (!notificationWindow)
    {
        int width = [[NSScreen mainScreen] frame].size.width;
        int height = [[NSScreen mainScreen] frame].size.height;

        notificationWindow = [[NotificationWindow alloc] initWithRect:NSMakeRect(width - 420, height - 130, 400, 100)];
        NSNib *nib = [[NSNib alloc] initWithNibNamed:@"Notification" bundle: nil];
        NSArray *objects;
        [nib instantiateNibWithOwner:self topLevelObjects:&objects];
        [notificationWindow setContentView: [objects objectAtIndex:0]];

        [notificationWindow setTag:tag];         
        [self.notificationWindows addObject:notificationWindow];
    }

    // Display window
    [notificationWindow makeKeyAndOrderFront:nil];
    [notificationWindow display];
    notificationWindow.fadeOut = msFade;
    [notificationWindow setPrimaryText:lineOneText];
}

The view is the only thing in the xib file so I would have though the objectAtIndex:0 would have been OK, but I get an -[NSApplication setFrame:]: unrecognized selector sent to instance 0x100508500 exception at that line.

Update I replaced the line with this block of code:

    for (id obj in objects) {
        if ([[obj class] isSubclassOfClass:[NSView class]])
            [notificationWindow setContentView: obj];
    }
Justin808
  • 20,859
  • 46
  • 160
  • 265
  • Set a breakpoint right after your `nib instantiateNibWithOwner:self topLevelObjects:&objects];` line and tell us if your `objects` array has a count greater than zero. If it is, can you find out what kind of object (`className`?) the item at index zero is? [See the answer to this question](http://stackoverflow.com/questions/1144629/in-objective-c-how-do-i-test-the-object-type) to tell you how to do this. – Michael Dautermann Jan 26 '12 at 02:02
  • @MichaelDautermann - there are 2 object, but the order seems to be random. My posted code works sometimes and not others. I'll post an edit that fixed the issue. – Justin808 Jan 26 '12 at 02:12
  • `[[obj class] isSubclassOfClass:[NSView class]]` should be written as `[obj isKindOfClass:[NSView class]]` – user102008 Jun 04 '12 at 19:12

1 Answers1

1

I vaguely recall that nibs have a hidden 1st object that represents the first responder (or something). Try objectAtIndex:1 instead and see if that works.

Nick Lockwood
  • 40,865
  • 11
  • 112
  • 103
  • There are 2 objects in the array, setting to 1 works, sometimes, and 0 sometimes, so it looks like the order of objects in the array is random. – Justin808 Jan 26 '12 at 02:05
  • I've marked this right as it led me to the answer, as did Michael's comment above. – Justin808 Jan 26 '12 at 02:14