2011-04-05 29 views

回答

42

如果你想创建一个新窗口中单独的一类,这些步骤如下:

  1. 创建一个类,这是一个子类NSWindowController例如中NewWindowController
  2. 为NewWindowController类创建一个窗口xib。
  3. 在按钮点击代码为:

    NewWindowController *windowController = [[NewWindowController alloc] initWithWindowNibName:@"You Window XIB Name"]; 
    [windowController showWindow:self]; 
    
+0

谢谢你......它工作得很好...... :)你解释得很好......谢谢......我在寻找几天,现在它工作得很好...... :) – ShinuShajahan 2011-04-06 03:56:28

+5

我试过同样的东西,但我遇到的问题是新窗口在显示后立即关闭。我没有任何代码,所以我不知道我做错了什么。有任何想法吗? – Interfector 2012-04-18 22:34:59

+0

@Interfector我有同样的问题..你弄明白了吗? – 2012-05-05 08:58:03

11
NSWindowController * wc=[[NSWindowController alloc] initWithWindowNibName:@"your_nib_name"]; 
[wc showWindow:self]; 
+0

@ SAURABH哇!谢谢一堆!它的工作......谢谢你...我是新来的stackoverflow,这是非常令人印象深刻的...... :) – ShinuShajahan 2011-04-06 03:53:21

+0

其实我在过去几天寻找相同的! :)我尝试了不同的但没有子类NSWindowController! – ShinuShajahan 2011-04-06 04:10:12

+0

为什么这个子分类答案比这个分类的评分更高?当然,不是继承是一个更简单,更优雅的答案? – fatuhoku 2013-08-09 19:38:38

6
  1. 创建类,这是一类子例如NSWindowController的NewWindowController
  2. 为NewWindowController类创建一个窗口xib。
  3. 在点击按钮的代码为:

    NewWindowController *controllerWindow = [[NewWindowController alloc] initWithWindowNibName:@"You Window XIB Name"]; [controllerWindow showWindow:self];

是的,但如果这个代码是一些FUNC内的窗口关闭。 这里是解决方案。

blah.h

@interface blah : NSObject { 
    ... 
    NewWindowController *controllerWindow; 
    ... 
} 

blah.m

@implementation 
... 
    -(IBAction)openNewWindow:(id)sender { 
     controllerWindow = [[NewWindowController alloc] initWithWindowNibName:@"You Window XIB Name"]; 
     [controllerWindow showWindow:self]; 
    } 
... 
+2

最好给现有答案添加评论(如果可以的话),而不是引用评论,回复并发布自己的评论。 – 2013-09-03 00:05:40

+1

@AdrianWragg,是的,我是新手,所以我很抱歉。 – WildMassacre 2013-09-03 18:38:29

+1

呃......我怎样才能关闭前一个?此代码一次打开两个窗口。 – mthama 2014-08-05 22:25:09

5

斯威夫特3:在你的故事板去WindowController - >身份检查 - > storyBoardID:填写:主窗口。 然后从当前视图 - 控制链接按钮上的故事板下面的方法:

@IBAction func newWindow(_ sender: Any) { 
    let myWindowController = self.storyboard!.instantiateController(withIdentifier: "mainWindow") as! NSWindowController 
    myWindowController.showWindow(self) 
} 
相关问题