问题
I am trying to display an NSPopover in a mac application when the window opens to give the user an instruction, but the popover will not display.
If I call the exact same function from a button press then the popover successfully displays, but when I call it in windowDidLoad it doesn't.
I can see that the control I am presenting it from has a bounds, so I don't think that is the problem. I've also checked that the behaviour of the popover is not transient, so it shouldn't close without intervention.
In my function I'm passing some variables into a custom initialiser, but it is basically just this:
CustomViewController *instruction = [[CustomViewController alloc] init];
[instruction.popover showRelativeToRect:[aField bounds] ofView:aField preferredEdge:NSMaxXEdge];
The init method simply calls the following, and the custom view and controller are hooked up in the NIB.
[super initWithNibName:@"InstructionalView" bundle:nil]
Has anyone come accross this?
回答1:
You're programmatically allocating and init-ing your CustomViewController object, but the popover doesn't have a chance to load from the nib before you display it (on the line directly after the alloc & init).
Between those two lines, force a load via loadView, like this:
[instruction loadView];
You may also want to make certain that "instruction.popover
" isn't nil when you try to display it, too.
来源:https://stackoverflow.com/questions/18039909/open-nspopover-in-windowdidload