Linking an NSWindow in IB

2 views
Skip to first unread message

Guy Halford-Thompson

unread,
Aug 31, 2011, 1:30:26 AM8/31/11
to coco...@lists.apple.com
This is somewhat of a basic question, but I am having a lot of trouble
getting it to work.

When my app loads, I check for certain parameters, and then display an NSWindow.

I have created the window in IB and also created a custom class
(MyWindow). in IB I have given the window class MyWindow.

I have added a couple of extra functions to MyWindow and now I wish to
trigger the window to be displayed given certain conditions.

How can I display the window?

I have tried

MyWindow *win = [[MyWindow alloc] init];
[win makeKeyAndOrderFront:nil];

but it just displays a blank window...

I am pretty sure that I have not linked the window from IB properly
but I am really confused about how to do it and the apple
documentation / google don't seem to be able to help.

Thanks for any help


--
Guy Halford-Thompson
Blog                  - http://www.cach.me/blog
Twitter               - https://twitter.com/mrwooster
Google Plus       - http://gplus.name/guy
_______________________________________________

Cocoa-dev mailing list (Coco...@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/cocoa-dev-garchive-98506%40googlegroups.com

This email sent to cocoa-dev-ga...@googlegroups.com

Jens Alfke

unread,
Aug 31, 2011, 3:05:24 AM8/31/11
to Guy Halford-Thompson, coco...@lists.apple.com

On Aug 30, 2011, at 10:30 PM, Guy Halford-Thompson wrote:

> I have created the window in IB and also created a custom class
> (MyWindow). in IB I have given the window class MyWindow.
>
> I have added a couple of extra functions to MyWindow and now I wish to
> trigger the window to be displayed given certain conditions.
>
> How can I display the window?

You need to know which object is being called when the condition is met, and that object has to have a pointer to your window so it can call it. Generally you’d wire a connection in Interface Builder from that object to your window, so the object will have an instance variable pointing to the window.

In a simple app the object that’s doing stuff is most likely the application delegate object, so you’d add an instance variable of type MYWindow*, put the IBOutlet keyword on it so IB knows it can be wired to, and then control-drag a connection from the window to the app delegate. Now when a method of the app delegate is called (like some menu-triggered action) it can use that instance variable to call the window.

—Jens_______________________________________________

Ron Fleckner

unread,
Aug 31, 2011, 6:26:48 AM8/31/11
to Guy Halford-Thompson, coco...@lists.apple.com

On 31/08/2011, at 3:30 PM, Guy Halford-Thompson wrote:

> This is somewhat of a basic question, but I am having a lot of trouble
> getting it to work.
>
> When my app loads, I check for certain parameters, and then display an NSWindow.
>
> I have created the window in IB and also created a custom class
> (MyWindow). in IB I have given the window class MyWindow.
>
> I have added a couple of extra functions to MyWindow and now I wish to
> trigger the window to be displayed given certain conditions.
>
> How can I display the window?
>
> I have tried
>
> MyWindow *win = [[MyWindow alloc] init];
> [win makeKeyAndOrderFront:nil];
>
> but it just displays a blank window...
>
> I am pretty sure that I have not linked the window from IB properly
> but I am really confused about how to do it and the apple
> documentation / google don't seem to be able to help.
>
> Thanks for any help
>

Guy, the instance of MyWindow in the nib file is the one you want to show. So, you don't want to make another window in code as you have above. The [[Classname alloc] init...] construct is one way to create instances of a class in code. But you already have an instance in the nib file. All you need to do is make a connection to your instance of MyWindow in IB. First declare an IBOutlet in your nib's File's Owner class, then in IB, control-drag from the instance to File's Owner. This gives you a reference to that instance in your code.

HTH,

Ron

PS You can also, in IB, simply check the box in the MyWindow instance's inspector panel to make it visible at launch.

Guy Halford-Thompson

unread,
Aug 31, 2011, 10:54:41 PM8/31/11
to coco...@lists.apple.com
Hi,

Thanks for the help, I really appreciate it.

I still appear to be having some trouble tho.

Here is my code:

MyWindow.h
----------------
#import <Cocoa/Cocoa.h>

@interface MyWindow : NSObject <NSWindowDelegate> {

IBOutlet NSWindow *window;
}

-(void)showWindow:(id)sender;

@property (assign) IBOutlet NSWindow *window;

@end

MyWindow.m
-------------------

#import "MyWindow.h"

@implementation MyWindow

@synthesize window;

-(void)showWindow:(id)sender {
NSLog(@"**Window %@",window);
[window makeKeyAndOrderFront:nil];
}

@end


Then... in my main app delegate....

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

if {...condition is met....} {
MyWindow *uni = [[MyWindow alloc] init];
[uni showWindow:nil];
}
}

But its still not working...

Sorry if this is pretty basic, but getting my head round objective C
is proving to be difficult.

Thanks

Jens Alfke

unread,
Aug 31, 2011, 11:04:30 PM8/31/11
to Guy Halford-Thompson, coco...@lists.apple.com
MYWindow isn’t a window, it’s an NSObject that can act as a window delegate. So creating one isn’t going to create a window. You don’t have any code that creates a window; there’s a window property but nothing that sets it. Also, you’re calling a -showWindow: method on your MyWindow class, but you never defined or implemented such a method.

What you want to do is

- Add a window to your main nib/xib file.
- Add a MyWindow object too (drag in a generic object and set its class to MyWindow)
- Wire the window up to the MyWindow (control-drag from MyWindow to the NSWindow and select the “window” outlet)
- Create another IBOutlet variable in your app delegate’s class, of type MyWindow* … let’s say it’s named myWindow.
- Wire that up to the MyWindow object in the nib.
- Now to display the window your app delegate calls [[myWindow window] makeKeyAndOrderFront: self]

I haven’t looked at the tutorials and intro docs in a while, but there must be some that cover this stuff.

—Jens_______________________________________________

Conrad Shultz

unread,
Aug 31, 2011, 11:06:08 PM8/31/11
to Guy Halford-Thompson, coco...@lists.apple.com
Sorry I'm on my phone so I can't write a detailed response, but you're just structuring your app kind of backwards.

In IB you would want to have a connection between your custom window and an outlet in your app delegate in this case. You would then send the window an "order front" message from the app delegate.

Putting an outlet to your window _inside the window_ is counterproductive. (I assume also that you meant for MyWindow to be a subclass of NSWindow, not NSObject.)

(Sent from my iPhone.)

--
Conrad Shultz

> http://lists.apple.com/mailman/options/cocoa-dev/conrad%40synthetiqsolutions.com
>
> This email sent to con...@synthetiqsolutions.com

Guy Halford-Thompson

unread,
Sep 1, 2011, 12:18:31 AM9/1/11
to coco...@lists.apple.com
Thanks for all the responses.

I have managed to get it working using Jens response... it seems I got
a bit confused about how the linking via IB goes.

@Ron, thanks for the recommendation, I am finding it very difficult to
find good resources for Cocoa programming, and the apple developer
docs are not exactly easy to follow.

Many thanks

Guy

On 31 August 2011 20:06, Ron Fleckner <ronfl...@ozemail.com.au> wrote:
> Hello Guy,
>
> It's good that you posted some more code.  Your 'MyWindow' object is a subclass of NSObject.  Even if it was a subclass of NSWindow, which I think is what you want (a window, right?), you still set up a separate window in code with alloc-init, which in any case is not a window but an NSObject.  Really, at this point, you can't keep asking questions without first doing some basic homework yourself. I doubt that people here will bother answering your questions until you've done that.
>
> I recommend Aaron Hillegas's excellent 'Cocoa Programming For Mac OS X'

>> http://lists.apple.com/mailman/options/cocoa-dev/ronfleckner%40ozemail.com.au
>>
>> This email sent to ronfl...@ozemail.com.au
>
>

--
Guy Halford-Thompson
Blog                  - http://www.cach.me/blog
Twitter               - https://twitter.com/mrwooster
Google Plus       - http://gplus.name/guy

Reply all
Reply to author
Forward
0 new messages