0

I am having a very difficult time switching between forms in Cocoa interfaces. From my initial form and its delegate, I can hide the initial window then load and display the second window with all properties on it. This is working working... Alas, on attempting to return to the initial window, I hide the second window and the initial does not return...

Here are my .h and .m for initial form and for 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

Here are .h and .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
Undo
  • 25,519
  • 37
  • 106
  • 129
  • Before getting an answer, there's a couple problems with the code posted. in BtnBackActionPreformed method, you are referencing _frmTwo, which is not defined anywhere. setIsVisible is also not a method on NSWindow or NSWindowController. So I'm not sure what you are doing there. Additionally it's confusing calling your classes delegates when they inherit NSWindowController. FormTwoController would be a more 'Cocoa' name to call your window controller class. – Ryan Nichols May 24 '13 at 05:37

1 Answers1

0

Here's a simpler way to achieve what you are doing. I'm not going to write the .h definitions, just infer what the variables represent from their names.

- (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];
    }    
}

In your nib, make sure both windows have the 'Release when closed' checkbox unchecked so your window is not released when you call close. In your second FormTwo window controller, you should call the BtnSwitchAction from your BtnBackActionPreformed method.

I know there's a number of ways you can connect the window switching code to the back button, but I recommend having all the window switching logic in one method on the AppDelegate rather than manipulating OTHER windows from your BtnBackActionPreformed. That controller and method shouldn't know about the details of the other windows, it should just tell the AppDelegate to do the switch.

Ryan Nichols
  • 298
  • 1
  • 8