1

I still have a lot to learn with cocoa so I may have missed something obvious here. I have a custom view I would like to display in an nssplitview which replaces the current subview there.

I have a MessageView.xib file, and a MessageView .h/.m which subclasses NSView. I created a custom view instance for my main window (the one which contains the nssplitview) through Xcode 4's built in gui builder. I created an outlet to this instance of MessageView in my window's controller.

In my controller for the window when I want to swap out the subview for the splitview it runs this

 [splitView replaceSubview:[[splitView subviews] objectAtIndex:1] with:viewMessage];

viewMessage is the outlet to the MessageView.

When this code is run the display of that subview changes to be blank. I'm not sure if there is something wrong with my custom view or there is some size issue. Is there something I need to do to fit the view into the split screen view or is my custom view just not displaying correctly? I have had a difficult time finding a tutorial on creating custom subviews with Xcode 4 so I'm not sure if something could be wrong with that. The custom view just has a label and a textfield in it.

JonF
  • 2,336
  • 6
  • 38
  • 57

1 Answers1

3

Generally, you shouldn't need to replace NSSplitView's subviews with your own. Rather, you add your own custom view(s) as child views of the default subviews on each side of the divider. You can do this in code with addSubview:, but it's probably easier to just use Interface Builder in Xcode. Drag a "Custom View" into the splitview, then in the Identity Inspector, under Custom class, change the class to the name of your custom NSView subclass:

enter image description here

I think (off the top of my head, not tested), if you really do need to replace the default NSSplitView subviews with your own class, you can probably do it in Interface Builder using this same method, but selecting the default subview itself and changing its class in the inspector. This doesn't work for all AppKit classes, but it may work for NSSplitView.

Andrew Madsen
  • 21,309
  • 5
  • 56
  • 97
  • This doesn't really answer the question here. You are suggesting how to use a subclass, but the question is how to substitute or replace the existing view at runtime. I have this same need. (As did Wakjob below.) I'll probably post again here when I get this working. – livingtech May 22 '12 at 18:55
  • 1
    It wasn't (still isn't) clear why the OP wants to replace the split view's default subview with his own at runtime instead of adding his own view as a child view, which is the standard approach. I don't argue that there's never a reason to do that, but when a self-described beginner asks for advice, I tend to steer them toward the standard approach unless there's a clear reason to do something more complicated. Unlike the question you linked, he doesn't clearly state that he's switching between multiple views at runtime. – Andrew Madsen May 22 '12 at 19:59