首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >awakeFromNib被调用两次,并且未设置视图控制器内部的出口。

awakeFromNib被调用两次,并且未设置视图控制器内部的出口。
EN

Stack Overflow用户
提问于 2015-12-28 23:43:03
回答 1查看 154关注 0票数 0

我正在尝试为OSX构建一个菜单应用程序。我有一个AppDelegate,一个故事板和一个ViewController。故事板包含一个带有AppDelegate的应用程序场景。AppDelegate设置整个视图层次结构。在AppDelegate中,我用ContentViewController(ContentViewController)定义了一个NSPopover。为了设置ContentViewController,我使用以下代码从情节提要加载ViewController:

代码语言:javascript
复制
 NSStoryboard*board=[NSStoryboard storyboardWithName:@"Main" bundle:nil];
    _ruleView.contentViewController=[board instantiateControllerWithIdentifier:@"egm"];

当我使用调试器检查awakeFromNib方法时,ViewController中的出口没有被设置,为什么?下一个问题是当单击NSStatusItem时,它会显示NSPopover和NSPopover再次在my ViewController中调用awakeFromNib,然后我的应用程序崩溃。下面是AppDelegate的代码:

代码语言:javascript
复制
#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
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-03 14:54:36

我发现,在将视图层次结构从nib中解压缩时,可以多次调用awakeFromNib。一个更好的选择是viewDidLoad,它确保了整个视图层次结构的加载和准备。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34502427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档