在Windows XCode接口之间来回切换

时间:2013-05-23 18:25:33

标签: xcode cocoa user-interface switching

我很难在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

这是.h和.m for frmTwo

·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

1 个答案:

答案 0 :(得分: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”复选框,以便在您关闭时不会释放窗口。在第二个FormTwo窗口控制器中,您应该使用BtnSwitchAction方法调用BtnBackActionPreformed

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

相关问题