MacOSX应用开发:窗口、菜单与模态框的实现与应用
立即解锁
发布时间: 2025-08-25 02:22:39 阅读量: 2 订阅数: 7 

### Mac OS X 应用开发:窗口、菜单与模态框的实现与应用
#### 1. 窗口创建与控制器子类化
在开发过程中,应用的新按钮每次按下都能创建新窗口。实际上,每次点击按钮都会创建一个新的 `NSWindowController` 实例,该实例会加载 nib 文件的全新副本,其中包含文件内的所有对象。
若需要在控制器类中添加自定义代码,可以轻松对 `NSWindowController` 进行子类化。以下是具体步骤:
1. **创建子类**:在 Xcode 的 `WindowLab` 项目中,通过菜单选择 `File - New File...` 来创建名为 `NotSoEasyWindowController` 的 `NSWindowController` 子类。
2. **设置初始化方法**:为了让使用该类的人只需了解类本身,而无需知道 nib 文件的名称,可在 `init` 方法中构建对 nib 文件的调用。以下是具体代码:
```objc
- init {
if ((self = [super initWithWindowNibName:@"NotSoEasyWindow"])) {
[self window];
}
return self;
}
```
3. **添加功能方法**:为窗口控制器类添加一个简单功能,即创建一个方法,当调用该方法时,计算机发出蜂鸣声。在新类的 `.h` 和 `.m` 文件中添加以下代码:
```objc
// add this to NotSoEasyWindowController.h:
- (IBAction)beep:(id)sender;
// add this to NotSoEasyWindowController.m:
- (IBAction)beep:(id)sender
{
NSBeep();
}
```
4. **在应用委托类中添加方法**:在 `WindowLabAppDelegate` 类中添加一个方法来加载新窗口,并在 `.m` 文件顶部添加 `#import` 指令,以便编译器在编译应用委托时了解新类。
```objc
// add this to WindowLabAppDelegate.h:
- (IBAction)loadNotSoEasyWindow:(id)sender;
// add this to WindowLabAppDelegate.m:
- (IBAction)loadNotSoEasyWindow:(id)sender
{
[[NotSoEasyWindowController alloc] init];
}
#import "NotSoEasyWindowController.h"
```
5. **在 Interface Builder 中配置**:
- 打开 `MainMenu.xib`,将窗口稍微拉长,复制最后一个按钮并命名为 “Not So Easy”,然后将其连接到应用委托的 `loadNotSoEasy:` 动作方法。
- 创建一个新的空文件并保存为 `NotSoEasyWindow.xib`,从库中拖出一个窗口并命名为 “Not So Easy Window”,再拖入一个按钮并命名为 “Beep”。
- 选择 `File's Owner` 图标,打开 `Identity Inspector`,将其类设置为 `NotSoEasyWindowController`。然后从新添加的按钮 `Control-drag` 到主 nib 窗口中的 `File's Owner` 图标,并选择 `beep:` 方法。
#### 2. 模态窗口的使用
模态窗口是一种特殊的 GUI 元素,它会使应用进入特定 “模式”,在此模式下,应用仅接受模态窗口控件的输入,点击应用的其他位置只会发出蜂鸣声。由于其具有干扰性,应谨慎使用,通常在应用需要用户回答问题才能继续前进时使用。
##### 2.1 NSAlert 函数
Cocoa 中最简单的模态窗口是警报面板,可通过单个函数调用创建和运行。函数在用户点击按钮后返回,可通过返回值确定用户点击的按钮。常用的模态警报函数是 `NSRunAlertPanel`,它有几个变体,如 `NSRunCriticalAlertPanel` 和 `NSRunInformationalAlertPanel`。这些函数通常接受五个或更多参数,包括面板标题、显示文本和按钮标题。
以下是在 `WindowLabAppDelegate.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(@"Result:", @"You pressed the default button", nil, nil, nil);
break;
case NSAlertAlternateReturn:
NSRunInformationalAlertPanel(@"Result:", @"You pressed the alternate button", nil, nil, nil);
break;
case NSAlertOtherReturn:
NSRunInformationalAlertPanel(@"Result:", @"You pressed the other button", nil, nil, nil);
break;
default:
break;
}
}
```
要测试这些警报面板,可在 `MainMenu.xib` 中添加一个按钮,命名为 “Run Modal Alerts”,并将其连接到应用委托的 `runModalAlerts:` 动作方法。
####
0
0
复制全文
相关推荐








