?
Solved

Retaining an NSWindow size and state - looking for an elegant way to do this

Posted on 2010-08-13
4
Medium Priority
?
659 Views
Last Modified: 2013-12-26
I have a controller class which spawns a window when a certain condition occurs. This window can be closed by the user, moved around or resized.

Ideally, when this condition occurs, I'd like the window to re-open in the same spot the user closed it last time.

Looking for an elegant way to do this. Pointers (or references) will be most welcome.
0
Comment
Question by:ronenmagid1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33430319
I'm not sure I understand. You want to know how to move? setFrame, I think. The parameter is NSRect.

0
 

Author Comment

by:ronenmagid1
ID: 33430359
No, I can do that myself.

This is why I said "elegant". I thought there might be some Cocoa class that takes the snapshot of the windows location and/or if it's currently miniaturized and saves it into a pfile or something.
0
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 2000 total points
ID: 33430432
Animation?
I attached an action method as an example.
Full example is here:
http://www.wmdeveloper.com/2010/07/cocoa-implicit-animation.html

- (IBAction)move: (id) sender
{
    // The button frame.
    NSRect senderFrame = [sender frame];
    
    //The view bounds. The button belongs to this view.
    NSRect superBounds = [[sender superview] bounds];
    
    // Calculate new position within the view bounds.
    senderFrame.origin.x = (superBounds.size.width -
                            senderFrame.size.width) * drand48();
    senderFrame.origin.y = (superBounds.size.height -
                            senderFrame.size.height) * drand48();
    
    // Move the button.
    //[sender setFrame: senderFrame];
    [[sender animator] setFrame: senderFrame];
}

Open in new window

0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33430515
Do you need to store the rectangle somewhere and then read it and restore the view position?
Use NSUserDefaults. An example attached.


#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[NSDate date] forKey:@"Date and Time"];
    [defaults synchronize];
	
    NSDate *today = [defaults objectForKey:@"Date and Time"];
    NSLog(@"Saved data: %@", today);
	
    [pool drain];
    return 0;
}

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question