0

我在 Cocoa 界面中的表单之间切换非常困难。从我的初始表单及其委托中,我可以隐藏初始窗口,然后加载并显示第二个窗口及其所有属性。这正在工作......唉,在尝试返回初始窗口时,我隐藏了第二个窗口并且初始窗口没有返回......

这是我的 .h 和 .m 用于初始形式和 formTwo ......

。H

#import <Cocoa/Cocoa.h>
@class frmTwoDelegate;

@interface AppDelegate : NSObject {
@private
    frmTwoDelegate *_frmTwo;
}

@property (assign) IBOutlet NSWindow *window;
- (IBAction)BtnSwitchAction:(id)sender;
@end

.m

#import "AppDelegate.h"
#import "frmTwoDelegate.h"
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    ...
}
- (IBAction)BtnSwitchAction:(id)sender {
    if (!_frmTwo) {
        _frmTwo = [[DecriptDelegate alloc] initWithWindowNibName:@"frmTwo"];
        [_frmTwo setFrmStart:self];
    }
    [_frmTwo showWindow:sender];
    [_window setIsVisible:NO];
}
@end

这是 frmTwo 的 .h 和 .m

。H

#import <Cocoa/Cocoa.h>
@class AppDelegate;
@interface frmTwo : NSWindowController{
@private
    AppDelegate *frmStart;
    __unsafe_unretained NSTextView *_TxtView;    
}
@property (retain) AppDelegate *frmStart;
@property (assign) IBOutlet NSWindow *frmTwo;
@property (unsafe_unretained) IBOutlet NSTextView *TxtView;
- (IBAction)BtnOpenActionPreformed:(id)sender;
- (IBAction)BtnBackActionPreformed:(id)sender;
@end

.m

#import "frmTwo.h"
#import "AppDelegate.h"
@implementation frmTwo
@synthesize frmStart;
- (id)initWithWindow:(NSWindow *)window
{
    ...
}
- (void)windowDidLoad
{
   ...
}
- (IBAction)BtnOpenActionPreformed:(id)sender 
{
   ...
}
- (IBAction)BtnBackActionPreformed:(id)sender {
    [frmStart ShowWindow];
    [_frmTwo setIsVisible:NO];
}
@end
4

1 回答 1

0

这是实现您正在做的事情的一种更简单的方法。我不打算编写 .h 定义,只是从变量名中推断变量代表什么。

- (IBAction)BtnSwitchAction:(id)sender {
    if (!_formTwo) {
        _formTwo = [[DecriptDelegate alloc] initWithWindowNibName:@"frmTwo"];
        [_formTwo setFrmStart:self];
    }
    if(_formOne.isVisible) {
        [_window close];
        [_formTwo showWindow:sender];        
    } else if(_formTwo.isVisible) {
        [_formTwo close];
        [_window showWindow:sender];
    }    
}

在您的笔尖中,确保两个窗口Release when closed都未选中“”复选框,以便在您调用 close 时不会释放您的窗口。在您的第二个 FormTwo 窗口控制器中,您应该BtnSwitchAction从您的BtnBackActionPreformed方法中调用。

我知道有多种方法可以将窗口切换代码连接到后退按钮,但我建议在 AppDelegate 上的一种方法中使用所有窗口切换逻辑,而不是从BtnBackActionPreformed. 该控制器和方法不应该知道其他窗口的详细信息,它应该只告诉 AppDelegate 进行切换。

于 2013-05-24T05:56:08.183 回答