Mac开发:窗口、菜单与模态框的使用指南
立即解锁
发布时间: 2025-08-25 02:21:25 阅读量: 3 订阅数: 8 


Mac上学习Cocoa:从零开始的全面指南
### Mac开发:窗口、菜单与模态框的使用指南
#### 1. 自定义窗口控制器
在开发过程中,每次创建新的`NSWindowController`实例时,它会加载一个新的nib文件副本,其中包含了文件内的所有对象。通常情况下,我们可能需要在控制器类中添加一些自定义代码,这时可以通过子类化`NSWindowController`来满足需求。以下是具体操作步骤:
1. **创建子类**:在Xcode的`WindowLab`项目中,选择`File ➤ New File`,从模板选择器的OS X部分选择`Cocoa`,再选择`Objective-C class`,指定创建`NSWindowController`的子类。将类名从Xcode建议的`WLWindowController`改为`WLNotSoEasyWindowController`,并勾选`With XIB for user interface`,Xcode会自动生成对应的`.xib`文件。
2. **修改初始化方法**:为了让使用该类的人只关注类本身,而无需了解nib文件的名称,我们可以在初始化方法中直接指定nib文件的名称,并在初始化时加载窗口。代码如下:
```objc
- init
{
if ((self = [super initWithWindowNibName:@"WLNotSoEasyWindowController"])) {
[self window];
}
return self;
}
```
同时,移除Xcode生成的`initWithWindow:`方法,因为它在这种情况下不需要被调用。
3. **添加按钮和动作**:打开`MainMenu.xib`,将窗口稍微拉高,复制最后一个按钮并命名为“Not So Easy”。在助理编辑器中打开`WLAppDelegate.h`,从按钮Control-drag到`WLAppDelegate.h`,添加一个名为`loadNotSoEasyWindow`的新动作。在`WLAppDelegate.m`中实现该动作:
```objc
// add this to WLAppDelegate.m:
- (IBAction)loadNotSoEasyWindow:(id)sender
{
WLNotSoEasyWindowController *notSoEasyController = [[WLNotSoEasyWindowController alloc] init];
[self.subWindows addObject:notSoEasyController];
}
```
并在`WLAppDelegate.m`文件顶部添加`#import "WLNotSoEasyWindowController.h"`。
4. **添加功能方法**:为了确保窗口正确连接,我们可以添加一个简单的方法,使计算机在调用时发出蜂鸣声。在`WLNotSoEasyWindowController.h`中添加方法声明:
```objc
- (IBAction)beep:(id)sender;
```
在`WLNotSoEasyWindowController.m`中实现该方法:
```objc
#import "WLNotSoEasyWindowController.h"
@interface WLNotSoEasyWindowController ()
@end
@implementation WLNotSoEasyWindowController
- init
{
if ((self = [super initWithWindowNibName:@"WLNotSoEasyWindowController"])) {
[self window];
}
return self;
}
- (void)windowDidLoad
{
[super windowDidLoad];
// Implement this method to handle any initialization after your window controller's window has
// been loaded from its nib file.
}
- (IBAction)beep:(id)sender
{
NSBeep();
}
@end
```
打开`WLNotSoEasyWindowController.nib`文件,从对象库中拖出一个按钮,命名为“Beep!”,将其Control-drag到对象坞中的`File’s Owner`图标,选择`beep:`动作。保存更改并运行,点击新按钮将创建一个点击按钮会发出蜂鸣声的窗口。
#### 2. 模态窗口
模态窗口是一种特殊的GUI元素,它会使应用程序进入特定的“模式”,在该模式下,应用程序仅接受模态窗口控件的输入,点击应用程序的其他区域只会发出蜂鸣声。由于其具有干扰性,模态窗口应谨慎使用,通常在应用程序需要用户回答问题才能继续执行时使用。
##### 2.1 NSAlert函数
Cocoa中最简单的模态窗口是警报面板,通过一个函数调用即可创建和运行。函数在用户点击按钮后返回,我们可以通过检查返回值来确定用户点击了哪个按钮。常用的模态警报函数是`NSRunAlertPanel`,它有几个变体,如`NSRunCriticalAlertPanel`和`NSRunInformationalAlertPanel`,可根据Apple的用户界面指南用于特定目的。这些函数通常接受五个或更多参数,包括面板标题、显示文本和按钮标题。以下是一个示例,展示了如何在应用程序中使用警报面板:
1. **添加按钮和动作**:在`MainMenu.xib`中添加一个名为“Run Modal Alerts”的按钮,Control-drag到`WLAppDelegate.h`添加一个名为`runModalAlerts`的新动作。
2. **实现动作方法**:在`WLAppDelegate.m`中实现该动作:
```objc
- (IBAction)runModalAlerts:(id)sender
{
NSRunCriticalAlertPanel(@"Basic Usage", @"This is a plain alert panel.", nil, nil, nil);
NSRunAlertPanel(@"Three Buttons", @"We can set button titles:",
@"Really?", @"Oh, how delightful!", @"Whatever.");
NSRunAlertPanel(@"Formatting Strings", @"We can also do some formatting, %@ %@",
nil, nil, nil, @"putting values for insertion at the end,", @"after the three button values.");
switch (NSRunAlertPanel(@"Noticing The Selection",
@"And of course, we can detect which button is clicked.",
@"Default", @"Alternate", @"Other")) {
case NSAlertDefaultReturn:
NSRunInformationalAlertPanel
```
0
0
复制全文
相关推荐










