1

I'm showing an NSPopover in an NSView, originating from a point on an NSBezierPath. I'm able to show the popover without a problem, but I can't seem to set the string value of the two text fields in it. The popover and the content view are both a custom subclass of NSPopover and NSViewController, respectively. The NSPopover subclass is also the NSPopover's delegate, although I don't implement any delegate methods, so I'm not sure I even need to do that.

Here is my subclass of NSViewController:

#import <Cocoa/Cocoa.h>

@interface WeightPopoverViewController : NSViewController
@end

#import "WeightPopoverViewController.h"
@interface WeightPopoverViewController ()
@end

@implementation WeightPopoverViewController
- (id)init {  
  self = [super initWithNibName:@"WeightPopoverViewController" bundle:nil];
  if (self) {
  }
  return self;
}
@end

And my subclass of NSPopover:

#import <Cocoa/Cocoa.h>

@interface WeightPopoverController : NSPopover <NSPopoverDelegate> {
  NSTextField *dateLabel;
  NSTextField *weightLabel;
}
@property (strong) IBOutlet NSTextField *dateLabel;
@property (strong) IBOutlet NSTextField *weightLabel;
@end

#import "WeightPopoverController.h"

@implementation WeightPopoverController
@synthesize weightLabel;
@synthesize dateLabel;
@end

This is the code in my NSView subclass that opens up the popover:

@interface WeightGraphViewController () {
  WeightPopoverController *popover;
  WeightPopoverViewController *vc;
}

...

-(void)mouseEntered:(NSEvent *)theEvent {

  // initialize the popover and its view controller
  vc = [[WeightPopoverViewController alloc] init];
  popover = [[WeightPopoverController alloc] init];

  // configure popover
  [popover setContentViewController:vc];
  [popover setDelegate:popover];
  [popover setAnimates:NO];

  // set labels
  for (id key in (id)[theEvent userData]) {
    [popover.weightLabel setStringValue:[(NSDictionary*)[theEvent userData] objectForKey:key]];
    [popover.dateLabel setStringValue:key];
  }

  // set the location
  (redacted, irrelevant)

  // show popover
  [popover showRelativeToRect:rect ofView:[self window].contentView preferredEdge:NSMaxYEdge];
}

-(void)mouseExited:(NSEvent *)theEvent {

  [popover close];
  popover = nil;
}

In WeightPopoverViewController.xib, I've set the File's Owner to WeightPopoverViewController and connected the view to the custom NSView. In this xib I also have an Object set to WeightPopoverController with the dateLabel and weightLabel connected to their text fields and the contentViewController set to File's Owner.

I think where I am going wrong is likely related to how I have configured my class / instance variables for the NSPopover, but from the research I've done and documentation I've read I can't seem to crack where I've gone wrong. Any help would be appreciated.

UPDATE:

I removed the NSPopover subclass from code and from IB. I put my outlets in my NSViewController and connected them in IB. However, I'm still not able to set the string values. The following won't compile with the error "Property 'weightLabel' not found on object of type NSPopover*'".

@interface WeightGraphViewController () {
  NSPopover *popover;
  ...
}

-(void)mouseEntered:(NSEvent *)theEvent {

  vc = [[WeightPopoverViewController alloc] init];
  popover = [[NSPopover alloc] init];
  [popover setContentViewController:vc];
  [popover.dateLabel setStringValue:@"test"];
}

I have the property definition exactly as I had it in my NSPopover subclass, but now in my NSViewController. This is actually what I had before, and since I wasn't able to set the properties from the NSViewController, I figured I needed to do it through a subclass of NSPopover. This is why I thought I am having an issue with how I have configured my class / instance variables.

user4034838
  • 121
  • 9

2 Answers2

0

You seem to be creating two popovers, one in code (popover = [[WeightPopoverController alloc] init]) and one in Interface Builder (In this xib I also have an Object set to WeightPopoverController). Have a think about what you’re trying to achieve.

I would also advise against subclassing NSPopover. I believe this is causing confusion and is unnecessary. Instead, put the outlets to your dateLabel and weightLabel in the popover’s content view controller.

Douglas Hill
  • 1,537
  • 10
  • 14
0

I've experienced something that I think is similar. The root problem is that the "outlets" connecting your view (XIB) to your controller are not initialized until after the view has been displayed. If the controller tries to set properties on any UI controls in the view before the popover has been opened, those changes are ignored (since all the controls will be nil).

Luckily, there's an easy solution (as mentioned in this answer): just invoke the view getter on your controller, and it will force the view to initialize sooner.

In other words:

popover = [NSPopover new];
myController = [[MyViewController alloc] initWithNibName:@"MyView" bundle:nil];
popover.contentViewController = myController;

[myController view];  // force view to initialize
...set some values on myController...  // works because view is now loaded

[popover showRelativeToRect: ...];
Community
  • 1
  • 1
peterflynn
  • 4,667
  • 2
  • 27
  • 40