以编程方式关闭模态表目标C.

时间:2014-02-07 14:41:16

标签: objective-c macos cocoa

值得一提的是,我对目标c和可可编程非常陌生,所以这可能是一个非常愚蠢的问题。好的,所以我有窗口需要打开另一个窗口作为模型表。我有以下文件:

  • MainMenu.xib(显然是程序的主窗口)
  • AppDelegate.m(此处理在applicationDidFinishLaunching上打开Login.xib)
  • Login.xib(用作模态表的窗口,包含2个textFields和一个按钮)
  • LoginController.m(处理模态表中填写的数据)

AppDelegate.m:

#import "AppDelegate.h"

@implementation AppDelegate
@synthesize loginScreen = _loginScreen;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Prompt user login credentials
    [self activateLoginScreen];

}

-(IBAction)activateLoginScreen {

    if (!_loginScreen)
        [NSBundle loadNibNamed:@"Login" owner:self];

    [NSApp beginSheet:self.loginScreen
       modalForWindow:[[NSApp delegate] window]
        modalDelegate:self
       didEndSelector:NULL
          contextInfo:NULL];

}

- (IBAction)closeLoginScreen:(id)sender {

    [NSApp endSheet:self.loginScreen];
    [self.loginScreen close];
    self.loginScreen = nil;

}

我的LoginController.m:

#import "LoginController.h"

@implementation LoginController
@synthesize txtMnemonic, txtPassword;

- (IBAction)btnLogin:(id)sender {

    NSString *mnemonic = [txtMnemonic stringValue];
    NSString *password = [txtPassword stringValue];

    if ([mnemonic length] == 0 || [password length] == 0) {
        NSLog(@"Please provide username and/or password.");
    } else {

        // Call web service here

    }


}

@end

此处还值得一提的是,AppDelegate是Login.xib的文件所有者....

我的问题是,我可以在应用启动时打开模态窗口。但是,我如何从LoginController.m中关闭该窗口?并且无法通过某些按钮操作完成。该模态表上的按钮用于处理表单。因此,在表单处理完毕并且所有内容都已经过验证后,我是否要关闭模态表。所以基本上从LoginController.m中的AppDelegate.m中调用'closeLoginScreen()'

我希望这一切都有意义,请问是否需要进一步的信息。提前谢谢!

1 个答案:

答案 0 :(得分:0)

您可以在LoginController中使用

关闭工作表
[NSApp endSheet:[self window] returnCode:NSCancelButton]; //... or NSOKButton

虽然没有必要,但通常的做法是提供代理来执行清理!

-(IBAction)activateLoginScreen {

if (!_loginScreen)
    [NSBundle loadNibNamed:@"Login" owner:self];

[NSApp beginSheet:self.loginScreen
   modalForWindow:[[NSApp delegate] window]
    modalDelegate:self
   didEndSelector:@selector(mySheetWasClosed:returnCode:contextInfo:)
      contextInfo:NULL];
}

你的代表职能

- (void)mySheetWasClosed:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
    if (returnCode==NSOKButton)
    {
        // all validations have been made by the sheet
    }
    [sheet orderOut:self];
    self.loginScreen = nil;
}