How do I access the view of a Window from interfacebuilder?

I'm trying to make interfaces for a cocoa app in xcode's interface builder. The view of one of the windows I made in Interface builder depends on data from another view, so it is necessary to message this view from the class which must pass it data. However I can't seem to find a way to get a reference for this view object from the owner of the nib file. Here is roughly the code I'm using:

controller = [[NSWindowController alloc] initWithWindowNibName:@"Somenibname"];
[[controller window] display];
theOtherView = [[[[controller window] contentView] subviews] objectAtIndex:1];
[theOtherView setObjectwhichneedstobemessaged:self];
[theOtherView sendAMessage:self];

The object that this code is in never receives the message. Initially I actually thought that the contentview was the view that appears in interface builder and tried to get a reference to it like this

theOtherView = [[[controller window] contentView]];

but that didn't work either. Thanks for reading.

Answers


It sounds like you may need a better understanding of how the View-Controller structures work with nib files and without more code/detail its difficult to know exactly what you are trying to do, but a quick way to solve your problem could be to use NSNotification's instead of trying to locate the other view and instigate a message send via call chain.

You can register to handle a notification from the receiving view and send the notification from the instigating view (and vice versa if you need it two ways).


Read up on IBOutlet and consider linking the views that you need directly from Interface Builder.

For example, your NSWindowController subclass might have:

@interface MyWindowController : NSWindowController
{
  /* can also use more specific classes if you need them, e.g. NSButton if it's really an NSButton */
  IBOutlet NSView* firstViewIWant;
  IBOutlet NSView* secondViewIWant;
}
  . . .
@end

Your implementation might have:

- (void)
windowDidLoad
{
  [super windowDidLoad];
  /* make sure the views were connected properly */
  assert(nil != firstViewIWant);
  assert(nil != secondViewIWant);
    . . .
}

Then in Interface Builder, hook up these outlets from "File's Owner" to the exact views that you need them to be.


Need Your Help

Access Virtual Directory in Another Web Application

asp.net directory virtual

I have two web application projects (project A and project B) in the same solution.

Dropit Jquery - keep submenu open on click

javascript jquery

I'm using simple DropIt jquery dropdowns - http://dev7studios.com/dropit/