objective-c - 在 Cocoa 中呈现 XIB 的模式对话框 : best/shortest pattern?

标签 objective-c cocoa modal-dialog xib nswindowcontroller

下面是我的典型 WindowController 模块,用于呈现从 XIB 加载的模式对话框(可以是设置、询问用户名/密码等)。对于这样的事情来说似乎有点太复杂了。有什么想法可以更好地/用更少的代码来做到这一点吗?

别介意它是否要求输入密码,它可以是任何东西。最让我沮丧的是,我在每个基于 XIB 的模态窗口模块中重复相同的模式。这当然意味着我可以定义一个自定义窗口 Controller 类,但在这之前我需要确保这确实是最好的做事方式。

#import "MyPasswordWindowController.h"

static MyPasswordWindowController* windowController;

@interface MyPasswordWindowController ()
@property (weak) IBOutlet NSSecureTextField *passwordField;
@end

@implementation MyPasswordWindowController
{
    NSInteger _dialogCode;
}

- (id)init
{
    return [super initWithWindowNibName:@"MyPassword"];
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self.window center];
}

- (void)windowWillClose:(NSNotification*)notification
{
    [NSApp stopModalWithCode:_dialogCode];
    _dialogCode = 0;
}

- (IBAction)okButtonAction:(NSButton *)sender
{
    _dialogCode = 1;
    [self.window close];
}

- (IBAction)cancelButtonAction:(NSButton *)sender
{
    [self.window close];
}

+ (NSString*)run
{
    if (!windowController)
        windowController = [MyPasswordWindowController new];
    [windowController loadWindow];
    windowController.passwordField.stringValue = @"";
    if ([NSApp runModalForWindow:windowController.window])
        return windowController.passwordField.stringValue;
    return nil;
}

应用程序调用[MyPasswordWindowController run],因此从该模块的用户的角度来看,它看起来很简单,但当你往里面看时就不那么简单了。

最佳答案

在按钮上设置标签以区分它们。让它们都针对相同的操作方法:

- (IBAction) buttonAction:(NSButton*)sender
{
    [NSApp stopModalWithCode:[sender tag]];
    [self.window close];
}

删除 _dialogCode 实例变量和 -windowWillClose: 方法。

-[NSApplication runModalForWindow:] 已经使窗口居中,因此您可以摆脱 -awakeFromNib 方法。

摆脱对-[NSWindowController loadWindow]的调用。这是一个覆盖点。你不应该调用它。 The documentation在这一点上是明确的。当您请求窗口 Controller 的-window时,它将自动调用。

删除MyPasswordWindowController的静态实例。每次只需分配一个新的。保留旧窗口是没有意义的,并且重复使用窗口可能会很麻烦。

关于objective-c - 在 Cocoa 中呈现 XIB 的模式对话框 : best/shortest pattern?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20477717/

相关文章:

ios - 针对 AD FS 对 iOS 应用程序进行身份验证

iphone - 释放导航 Controller 堆栈中特定 UIVIewController 的内存

cocoa - NSView 未收到 mouseUp : event when mouse button is released outside of view

objective-c - 为什么 -[NSString 比较 :options:] return true when the strings are not equal?

jquery 空白/灰色显示对话框的页面

iphone - 如何检测圆形 View 内的触摸

ios - 在 iOS 应用程序中显示来自 Salesforce 的用户个人资料图片

ios - 以秒为单位的 UTC 到本地时间

javascript - 单击 Morris 饼图段时显示模态

javascript - vue-js-modal 对话框不关闭