Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6593625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:42:21+00:00 2026-05-25T17:42:21+00:00

I am making a simple mac app in which i want to switch windows.

  • 0

I am making a simple mac app in which i want to switch windows.
I have two NSWindowController class MainWindow and DetailWindow

I am using this code :

MainWindow class:

//MainWindow.h
@class DetailWindow;
@interface MainWindow : NSWindowController{

    IBOutlet NSButton *btn1;
    DetailWindow *detailwindow;
}
@property (nonatomic, retain) IBOutlet NSButton *btn1;
- (IBAction)btn1Event:(id)sender;

//MainWindow.m

@implementation MainWindow
@synthesize btn1;

- (IBAction)btn1Event:(id)sender {

if (!detailwindow) {
    detailwindow = [[DetailWindow alloc] initWithWindowNibName:@"DetailWindow"];

}
    [detailwindow showWindow:self];
}
@end

DetailWindow Class:

//DetailWindow.h
@class MainWindow;
@interface DetailWindow : NSWindowController{

    IBOutlet NSButton *backbtn;
    MainWindow *mainwindow;
}
@property (nonatomic, retain) IBOutlet NSButton *backbtn;
- (IBAction)back:(id)sender;

//DetailWindow.m

@implementation DetailWindow
@synthesize backbtn;

- (IBAction)back:(id)sender {

if (!mainwindow) {
    mainwindow = [[MainWindow alloc] initWithWindowNibName:@"MainWindow"];

}
    [mainwindow showWindow:self];
}
@end

Now the problem is when i click backbtn on DetaiWindow it will open a new MainWindow.
So i have two MainWindow on screen.
I want just main window at front when i click backbtn.

Any help??

Thank you..!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:42:22+00:00Added an answer on May 25, 2026 at 5:42 pm

    Your basic problem is that each window is assuming that it is its own job to create the other. Each has an ivar for the other, but there’s no external access to it — via a property or being an IBOutlet or anything else — so it always starts out as nil, and a new copy gets created instead of reusing the old one.

    There are any number of ways to get around this. Probably the easiest would be to create both windows in Interface Builder and link them up there, having made the ivars IBOutlet. Then you know you never have to create them in code at all.

    However, purely on the basis of inertia, here’s an alternative that sticks closer to what you’ve got already. Note that I’ve assumed for simplicity that mainWindow always exists first. If not, you’ll have to duplicate the process the other way around.

    //MainWindow.h
    @class DetailWindow;
    @interface MainWindow : NSWindowController
    {
    
        IBOutlet NSButton *btn1;
        DetailWindow *detailwindow;
    }
    @property (nonatomic, retain) NSButton *btn1;
    - (IBAction)btn1Event:(id)sender;
    
    //MainWindow.m
    
    @implementation MainWindow
    @synthesize btn1;
    
    - (IBAction)btn1Event:(id)sender
    {
        if (!detailwindow)
        {
            detailwindow = [[DetailWindow alloc] initWithWindowNibName:@"DetailWindow"];
    
            // having created the other window, give it a reference back to this one
            detailWindow.mainWindow = self;
        }
    
        [detailwindow showWindow:self];
    }
    @end
    
    //DetailWindow.h
    @class MainWindow;
    @interface DetailWindow : NSWindowController
    {
    
        IBOutlet NSButton *backbtn;
        MainWindow *mainwindow;
    }
    @property (nonatomic, retain) NSButton *backbtn;
    
    // allow the main window to be set from outside
    @property (nonatomic, retain) MainWindow *mainWindow;
    - (IBAction)back:(id)sender;
    
    //DetailWindow.m
    
    @implementation DetailWindow
    @synthesize backbtn;
    @synthesize mainWindow;
    
    - (IBAction)back:(id)sender
    {
        // no window creation on the way back
        NSAssert(mainWindow, "mainWindow not set!");
        [mainwindow showWindow:self];
    }
    @end
    

    Untested, so usual caveats apply.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok so I'm making a simple web app using the technologies from the topic,
I'm learning F#, and decided to try making simple XNA games for windows using
If I'm making a simple grid based game, for example, I might have a
I'm making a simple form to create polls, therefore I want the possibility to
How to create simple post/get API making simple calling to GAE database using Google
I'v been making a simple target card reading server on eclipse, on my mac
I want to try making a simple game engine. Just something that handles states,
I'm making simple XMPP client using Jabber-Net. To send simple message i'm using a
I'm making a free Mac app that is simply a wrapper over the purge
Making a simple map app, plan on adding buttons to mark specific locations But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.