18134

Interface Builder NSScrollView not working with IB defined View

Question:

I have defined a custom class with a drawRect called Drawing. I have laid out the view in my NIB to contain an NSScrollView with a Drawing subview. When I launch the program, the screen is blank. Interestingly, when I create the documentView for the NSScrollView programmatically, I get an image in my scroll view. When I use the instance for the nib in my setDocumentView, I get nothing.

So if the Drawing view is set in IB,

[_scrollViewWorkspace setDocumentView:_drawing]; //does not work.

But

[_scrollViewWorkspace setDocumentView:[[Drawing alloc] initWithFrame:NSMakeRect(0,0,[[_scrollViewWorkspace documentView ]bounds].size.width, [[_scrollViewWorkspace documentView] bounds ].size.height)]];

Works great!

Why can't I statically bind a drawing object in a NIB?

Answer1:

When you set the custom class of the NSView subclass in the nib file to your custom class, at runtime, an instance of that class will be created using initWithCoder: rather than initWithFrame:.

You can easily check to see if this is the case by adding the following method to your custom Drawing view:

- (id)initWithCoder:(NSCoder *)coder { NSLog(@"[%@ %@]", NSStringFromClass([self class]), NSStringFromSelector(_cmd)); return [super initWithCoder:coder]; }

Then launch your app and see if [Drawing initWithCoder:] is logged to the console.

If you have overridden initWithFrame: in your Drawing class to do custom initialization (like set a custom image automatically), you will need to add that same code to the initWithCoder: method so that you end up with the same result:

- (id)initWithCoder:(NSCoder *)coder { if ((self = [super initWithCoder:coder])) { // do custom initialization here } return self; }

Answer2:

Solved!

When I laid the view out in Interface Builder I just dragged the custom view to the NSScrollView in the canvas. I re-did the layout so that I used Embed->Scroll View and it worked.

Bruce

Recommend

  • How to send multiple buttons in button.addTarget action? Swift3
  • How to set image position programaticall in objective c?
  • How to stop the horizontal scrolling programmatically?
  • Trying to use CGContextAddArc… nothing is drawn?
  • error with Xcode 6 'Duplicate interface definition for class 'ViewController'
  • How to load image asynchronously using UIImageView+AFNetworking in ios
  • windowScriptObject method not found on Objective-C code
  • springboot + webpack dev server, does not change localhost bundle file after rebuilding
  • x-axis labels in coreplot not displayed
  • “A GKScore must specify a leaderboard.”
  • Is there any way to call saveCurrentTurnWithMatchData without sending a push notification?
  • Outlook to Excel hyperlink issue
  • Efficient User-Agent Regex to find Safari in Python
  • Get Currently Active User in Android
  • What is the correct way to synchronize a shared, static object in Java?
  • Two Tables Serving as one Model in Rails
  • Question about instantiating object
  • as3-flash: any way to access all the instances placed in different frames from document class?
  • how does System.Web.HttpRequest::PathInfo work?
  • Element.tagName for python not working
  • Spring Cloud Microservice Architecture Confusion
  • NSScanner Loop Question
  • gspread or such: help me get cell coordinates (not value)
  • How do I superscript characters in a UIButton?
  • How can I set a binding to a Combox in a UserControl?
  • SplFileObject error failed to open stream: No such file or directory
  • print() is showing quotation marks in results
  • Play WS (2.2.1): post/put large request
  • Refering to the class itself from within a class mehod in Objective C
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Highlight one bar in a series in highcharts?
  • Eraser for UIBezierPath
  • Do create extension work in single-user mode in postgres?
  • R: gsub and capture
  • Calling of Constructors in a Java
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Compare two NSDates in iPhone
  • Comma separated Values
  • PHP: When would you need the self:: keyword?
  • How to load view controller without button in storyboard?