Re: How to implement NSSplitViewDelegate methods in an NSSplitViewController subclass
Re: How to implement NSSplitViewDelegate methods in an NSSplitViewController subclass
- Subject: Re: How to implement NSSplitViewDelegate methods in an NSSplitViewController subclass
- From: Bill Cheeseman <email@hidden>
- Date: Sat, 08 Aug 2015 11:37:26 -0400
> On Aug 8, 2015, at 9:31 AM, Mike Abdullah <email@hidden> wrote:
>
> One thing you might want to watch out for that caught me out:
>
> Although NSSplitViewController is documented to be the split view’s delegate, I found that — at least for controllers created in IB — that relationship isn’t actually hooked up by default. I overrode delegate methods and they weren’t being called. So I manually made the controller be the split view’s delegate, and all works.
I've seen your conclusion about the delegate problem online, but I have gone about it a little differently and get a better result. Namely, I create my subclass of the split view controller programmatically, and the delegate is in fact connected automatically. Specifically, I create a custom split view in my main window's nib file (no storyboards) so that I can set up its constraints easily, and I create two custom views for the two split view subviews in their own separate nib files. I subclass NSSplitViewController programmatically as a Swift 2.0 singleton class, and in its convenience initializer, in this order (the order is critical), (1) I replace the two empty subviews in the window's nib file's custom split view with my two custom views from their nib files (instantiating and initializing their separate view controller subclasses in the process), (2) I replace my subclassed split view controller's default split view with the custom split view in the main window's nib file, (3) I call addChildViewController() twice with the custom subviews' view controller subclasses, and (4) I replace my subclassed split view controller's default view with the custom split view in the main window's nib file. That fourth step might seem unnecessary, but it has the effect of marking my subclassed split view controller's view as loaded (it sets viewLoaded to true behind the scenes) and as a result viewDidLoad() is never called. It also prevents a crash if I access the split view controller's 'view' property instead of 'splitView' for any reason This sounds complicated, but it takes only 5 lines of very simple code.
Then I implement several of the NSSplitViewDelegate delegate methods in my subclassed split view controller, and they are called exactly as documented -- even though I never set the split view's delegate programmatically or in the nib files. So I believe the documentation is correct, and the split view controller really is automatically made the split view's delegate (presumably in NSSplitViewItem). I think those who have done all this in a nib file or storyboard and discovered the delegate issue may have gotten something wrong in the preliminary setup -- which isn't surprising given how awful the documentation is.
I would be happy to be corrected, but this part of my code is now working flawlessly and it is as simple and elegant as anything I've every written. I'm still struggling with the other part, collapsing and uncollapsing the bottom subview of the splitview both with a toggle button and with double-clicking or dragging the divider. I have that working, too, but there is one last cosmetic glitch I haven't yet resolved.
--
Bill Cheeseman - email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden