how do i reference the window associated with a nswindowcontroller - macos

I have a subclassof NSWindowController with an associated xib file.
From my app delegate I display this using the following code:
if(!wc)
wc = [[NSWindowController alloc]initWithWindowNibName:#"MyNewWindowController"];
[wc showWindow:nil];
This displays the window. Now I want to reference that window in the new window controller but can't work out how. Specifically I have a button on the new window and I want to write something like:
- (IBAction)doStuffAndCloseWindow:(id)sender
{
[self doSomeStuff];
[*window* orderOut:nil];
}
I've tried creating a window variable (like the one created in appdelegate) but the compiler says my window variable is private.
So have do I declare and reference a window in my MyNewWindowController.m?
Thanks

That would be the 'window' method of NSWindowController. It's also a property that you can access via ".window".
So, in the first code snippet, that would be:
[wc window]
and in the second code snippet (assuming "doStuffAndCloseWindow" is part of your subclassed NSWindowController):
- (IBAction)doStuffAndCloseWindow:(id)sender
{
[self doSomeStuff];
[[self window] orderOut:nil];
}

Thanks to Michael, see above, first declare your subclassed NSwindowController thus
#property IBOutlet MyNewWindowController *wc;
Then in the implementation of the subclassed window controller, you can refer to the associated window with
[[self window] .....];
For example
[[self window] orderOut:self];

Related

The blue focus ring outside NSTextField missing?

In normal case, a blue retangle would appear outside a NSTextField object which becomes the first responder, like this image:
link for Normal Case
However, I got a NSTextField that have no the blue border outside. Why is that?
Here is how it happerns:
1> I create a typical MAC OS app.
2> I switch app's subview by calling the corresponding view's addSubview: and removeFromSuperview methods.
3> In one subview (which is actually the image referenced above) I click the "Next" button. Its action is something like this (defined in the subview's controller .m file):
- (IBAction)actionNextClicked:(id)sender{
//_hdlThreadNext is a NSThread object
[[_hdlThreadNext alloc] initWithTarget:self selector#selector(threadNext:) object:nil];
[_hdlThreadNext start];
}
And the thread is like:
- (void)threadNext:(id)sender{
#autoreleasepool{
BOOL success;
[CATransation begin];
/* send username and password and decrypt responce */
... // balabala... and set "success"
if (success){
[[self view] removeFromSuperview];
[self sendMessageToSuperview:#"Add Next View"]; // Superview's method, telling superview to call addSubview: to add another subview
}
else{
/* Nothing special to do */
}
[CATransation commit];
}
}
4> The subview switch to another one. Its combo view seemed to be OK: image for combo view
But the other NSTextView's blue border would NOT appear anymore!
Does Any guy know what wrong I had done? Thank you very much!
Perhaps I did totally wrong programming, so that few people met this problem.
I found a way to solve this problem. I mentioned that all (or most of?) the graphic changes should be done in main thread in a blog. Therefore I change the "if(success)" as:
if(success){
dispatch_async(dispatch_get_main_queue()' ^{
[[self view] removeFromSuperview];
[self sendMessageToSuperview:#"Add Next View"];
});
}
Solved, the focus rings come back.

Why I can't seem to modify my window properties when not using default XCode4 code structure?

Well, first of all, I hope I stated my problem correctly.
I am using Xcode4, but not using the default code structure.
I am aiming at creating a transparent window here. Now, it's very easy by just adding initialization code here :
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
[[self window] setAlphaValue:0.9];
[[self window] setOpaque: NO];
}
But, because I change the WindowAppDelegate into this :
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSLog(#"delegate: init main window");
mainWindow = [[MainWindowController alloc] initWithWindowNibName:#"MainWindow"];
[[mainWindow window] makeKeyAndOrderFront:self];
[[mainWindow window] setAlphaValue:0.8];
[[mainWindow window] setTitle:#"Fool"];
}
It won't work. I mean the code of setAlphaValue didn't had any effect, nor other method call such as setTitle.
I guess this is because of me didn't wire things correctly in the NIB...
But, what is it anyway?
Thanks in advance
#bavarious. This is his answer "Have you connected the window outlet in File’s Owner (which should be of type MainWindowController) to the actual window? – Bavarious Oct 30 at 7:37"
I will accept in in two days as the answer for my question.
Thanks #bavarious!

Nib's IBOutlets are not connected (nil)

I have a Custom class which is supposed to load a window from a nib file.
When I load the nib file everything looks fine except the IBOutlets are not connected i.e. nil. IBActions work fine and when they are called the IBOutlets are not nil anymore.
The class is added to the nib in the IB as an object and obviously everything wired up.
It's the file's Owner and delegate
When it loads the nib, the window appears only if its "visible at launch" is set.
It doesn't matter where I load the nib and try to access IBOutlets immediately or seconds later.
It must be something very trivial...
UPDATE2: I UPLOADED AN EVEN SIMPLER TRIAL PROJECT: Trial Project2
Expected behaviour: Window2 title changes to "Title has Changed x times" when loads. It only starts working once the button is pressed i.e. IBOutlets are not nil anymore.
The big change was subclassing NSWindowController to create MyClass. This way, you only attempt to manipulate the close button after the window has loaded. Your code was small enough that I thought it best to simply post the changes:
trialProjectAppDelegate.m
#import "trialProjectAppDelegate.h"
#implementation trialProjectAppDelegate
#synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
myclass = [[MyClass alloc] init];
// Note that I'm forcing the window to load here.
(void) [myclass window];
}
#end
MyClass.h
#import <Cocoa/Cocoa.h>
#interface MyClass : NSWindowController
{
IBOutlet NSButton *dismissButton;
}
- (IBAction)closeNaggingWindow:(id)sender;
- (void)disableDismissButton;
#end
MyClass.m
#import "MyClass.h"
#implementation MyClass
- (id)init
{
if ((self = [super initWithWindowNibName:#"Window"]) != nil)
{
}
return self;
}
- (void)disableDismissButton
{
[dismissButton setEnabled:NO];
[dismissButton setTitle:#"Closing enabled in 5 sec"];
[self performSelector:#selector(enableDismissButton:) withObject:nil afterDelay:5];
}
- (IBAction)enableDismissButton:(id)sender
{
[dismissButton setEnabled:YES];
[dismissButton setTitle:#"Close"];
}
- (IBAction)closeNaggingWindow:(id)sender
{
[[self window] close];
[self autorelease];
}
- (void)awakeFromNib
{
[self disableDismissButton];
}
#end
Finally, in your Window.xib file, discard the naggingWindow outlet and connect your window to the window outlet that NSWindowController provides.
I haven't worked with any of the OS X interface classes, so there may be some aspect of this that is not 100% precisely accurate, but basically what is happening is this:
You've wired your nib's NSWindow object to a MyClass object, which is also in your nib. So when you load that nib, here's what's happening:
A MyClass instance is created
An NSWindow instance is created, with several subviews. The NSWindow and the button are attached to the new MyClass instance.
Nothing is wired to the File's Owner pseudo-object (the MyClass instance you created in your app delegate)
Then -changeWindowTitle is called on your original MyClass instance, which has none of its outlets wired.
The solution is simple: remove the MyClass object from your nib file. Select the "File's Owner", and in the Identity Inspector (third icon from the left in the Utility pane) set "Class" to "MyClass". Now reconnect your outlets to the File's Owner object, which is your original MyClass instance. You should now see the behavior you expected.
As an aside, the right place to do things "as soon as the nib is loaded", like setting properties on your fresh IBOutlet objects, is in the method -windowDidLoad.

Dynamic object creation

I did created NSTableView, but this not displays on NSWindow. Code example:
NSTableView *tbl = [[NSTableView alloc] initWithFrame:NSMakeRect(0,0,500,500)];
[window.contentView addSubview:tbl];
P.S.
I need make application without IBOutlets only
That code is correct. Make sure that you have created or loaded the window (i.e., window is not nil) and that you have given it a content view (window.contentView is not nil).
Also, don't forget to release tbl.
Try:
NSTableView *tbl = [[NSTableView alloc] initWithFrame:NSMakeRect(0,0,500,500)];
[window setContentView:tbl];
[tbl release];
Assuming your window object isn't nil and that you've initialized it correctly.
Update: see here for info about making an NSWindow visible; specifically:
Opening a window—that is, making a window visible—is normally accomplished by placing the window into the application's window list by invoking one of the methods makeKeyAndOrderFront:, orderFront:
So, after the code above, added the following to make the window visible:
[window makeKeyAndOrderFront:self];
why don't you use uitableview?
it is standard class for table view.
UITableView *tbl = [[[UITableView alloc] initWithFrame:NSMakeRect(0,0,500,500)] autorelease];
[window.contentView addSubview:tbl];
[window makeKeyAndVisible];

Calling function from popover

Alright, so I made a popover from my main view and all that good stuff. But I want to have my popover call an action in my main view when a button within the popover is pressed.
MainView *mainView = [[MainView alloc] initWithNibName:#"MainView" bundle:nil];
[mainView doStuff];
The "dostuff" function changes some elements within the view. For example, the color of the toolbar is supposed to be changed. I've put a print command and the print command executes. But for some reason, the toolbar won't change color.
I've imported the header of MainView into the popover.
I did an #class thingy for MainView in my popover.
doStuff is declared in MainView's header.
The IBOutlets are declared too, and connected.
Any ideas?
Well its disappointing that we have no direct method that can be used to check in which view (view controller) the popover is shown. The thing that I am doing in tabbased application is:
New_iPadAppDelegate *appDel = (New_iPadAppDelegate *)[[UIApplication sharedApplication] delegate];
NSArray *viewConts = [(UINavigationController *)[[appDel tabBarController] selectedViewController] viewControllers];
MainViewController *viewController = (MainViewController *)[viewConts lastObject];
if([[viewController popoverController] isPopoverVisible]){
[viewController doStuff];
}
Hope this helps,
I know this is not the best way, hoping apple thinks about this issue, or if somebody has devised a work around.
Thanks,
Madhup

Resources