我正在尝试为OSX构建一个菜单应用程序。我有一个AppDelegate,一个故事板和一个ViewController。故事板包含一个带有AppDelegate的应用程序场景。AppDelegate设置整个视图层次结构。在AppDelegate中,我用ContentViewController(ContentViewController)定义了一个NSPopover。为了设置ContentViewController,我使用以下代码从情节提要加载ViewController:
NSStoryboard*board=[NSStoryboard storyboardWithName:@"Main" bundle:nil];
_ruleView.contentViewController=[board instantiateControllerWithIdentifier:@"egm"];当我使用调试器检查awakeFromNib方法时,ViewController中的出口没有被设置,为什么?下一个问题是当单击NSStatusItem时,它会显示NSPopover和NSPopover再次在my ViewController中调用awakeFromNib,然后我的应用程序崩溃。下面是AppDelegate的代码:
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate()
{
NSStatusItem*_statusItem;
NSPopover*_ruleView;
}
-(void)statusItemButtonPressed:(id)sender;
-(void)openPopupWindow;
-(void)closePopupWindow;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
}
-(id)init
{
self=[super init];
if(self)
{
_ruleView=[[NSPopover alloc] init];
NSStoryboard*board=[NSStoryboard storyboardWithName:@"Main" bundle:nil];
_ruleView.contentViewController=[board instantiateControllerWithIdentifier:@"egm"];
_statusItem=[[NSStatusBar systemStatusBar] statusItemWithLength:24];
_statusItem.button.title=@"EG";
_statusItem.button.action=@selector(statusItemButtonPressed:);
}
return self;
}
-(void)statusItemButtonPressed:(id)sender
{
if(!_ruleView.shown)
{
[self openPopupWindow];
}
else
{
[self closePopupWindow];
}
}
-(void)openPopupWindow{
[_ruleView showRelativeToRect:NSZeroRect ofView:_statusItem.button preferredEdge:NSMinYEdge];
}
-(void)closePopupWindow{
[_ruleView close];
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}发布于 2016-06-03 14:54:36
我发现,在将视图层次结构从nib中解压缩时,可以多次调用awakeFromNib。一个更好的选择是viewDidLoad,它确保了整个视图层次结构的加载和准备。
https://stackoverflow.com/questions/34502427
复制相似问题