2014-02-07 21 views
0

值得一提的是,我對客觀的c和可可編程非常陌生,所以這可能是一個非常愚蠢的問題。好吧,我有窗口需要打開另一個窗口作爲模型表。我有以下文件:以編程方式關閉模態圖表目標C

  • MainMenu.xib
  • AppDelegate.m(方案顯然主窗口)(此手柄上的applicationDidFinishLaunching打開Login.xib)
  • Login.xib(窗口即作爲模態片材,包含2個文本字段和一個按鈕)
  • 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()'

我希望所有這些都有意義,並且請只詢問是否需要進一步的信息。提前致謝!

+0

忽略我的答案,認爲這是IOS – Fonix

回答

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; 
} 
+0

謝謝您的回答。雖然,當你說[自定義窗口]時,LoginController不是Login.xib的文件所有者...所以我得到一個錯誤「在LoginController的界面上看不到'聲明選擇器'窗口' – Tiwaz89

+0

[self window ]就是一個例子,只需將它替換爲表單(窗口)對象的有效指針(在您的示例中該指針似乎是self.loginScreen,但我不確定誰是您的接口的所有者)。 – Merlevede