Note that there are some explanatory texts on larger screens.

plurals
  1. POProperties of CALayer in layer-backed view won't display, and variable losing value in cocoa app
    primarykey
    data
    text
    <p>I have a collection view containing objects (cards) bound to a core data entity. Each of those cards has a type attribute that gets set from user input when the card is added. The class of the Collection View Item Prototype is a subclass of NSView with a synthesized property "cardType". I've managed to get that synced up with the item represented. In InterfaceBuilder, the prototype View is connected as an outlet to the arrayController (*view). </p> <p>What's been elusive so far is that I want the cards in the collection view to have different colored backgrounds dependent on the type. I've been trying to get it to work by calling the following method from the array controller's <code>- (id)newObject</code> method:</p> <pre><code>-(void)setTypeOfCardView { NSLog(@"card type is %@",cardType); if(self.layer &amp;&amp; [self.cardType isEqual:@"Opening"]) { NSLog(@"yes to layer and card type test"); [self.layer setDelegate:self]; NSLog(@"borderwidth %f",self.layer.borderWidth); self.layer.backgroundColor = [NSColor blueColor].CGColor; self.layer.borderColor = [NSColor blackColor].CGColor; self.layer.borderWidth = 6.0; NSLog(@"borderwidth %f",self.layer.borderWidth); self.layer.cornerRadius = 5; [self.layer setNeedsDisplay]; } } </code></pre> <p>Feedback from the console is that the method is called, the cardType property is set correctly,and that when user input for adding a card with "Opening" type is called, it goes through and even seems to change the appropriate CALayer properties. (logs different attributes for the beginning and end of the method) For the life of me I can't get that to show up on the screen, though.</p> <p>I've also tried it with <code>[self drawRect:self.bounds];</code>, without `setNeedsDisplay', without any of them, all of them, etc, but I couldn't get anything to change on screen. </p> <p>I also tried another direction of putting the if block in the view's `drawRect:' method, like this:</p> <pre><code>- (void)drawRect:(NSRect)dirtyRect { //first test for layer if (self.layer){ NSLog(@"Layer Test"); self.layer.cornerRadius = 5; NSLog(@"card type is:%@",self.cardType); //begins nested cardType string comparison test NSString *label = @"Opening"; if ([self.cardType isEqual:label]) { NSLog(@"passed cardType string comparison test"); self.layer.backgroundColor = [NSColor blueColor].CGColor; } } NSLog(@"Drawing Card"); self.layer.borderColor = [NSColor blackColor].CGColor; self.layer.borderWidth = 6.0; self.layer.cornerRadius = 5; </code></pre> <p>} </p> <p>I can't get this to work either, but for different reasons. I can get the layer properties to change, but can't get the string comparison test to work, and when I log the cardType value to the console, now I get (null), instead of the proper string. The result from the code above is that the cards get the corner radius from the outer if block, the border properties from lower (outside the if blocks), but can't get the backgroundColor property from the inner nested if block. Unfortunately, that's where I would need all of it to be. </p> <p>As it seems like this is a problem with the cardType declaration, (or retaining the value?) I've included the header file for the class here:</p> <pre><code>#import &lt;Cocoa/Cocoa.h&gt; #import &lt;QuartzCore/QuartzCore.h&gt; @interface SDCardView : NSView{ } @property (strong) NSString *cardType; -(void)setTypeOfCardView; @end </code></pre> <p>I'm at my wits end here, so if you could throw me a bone, I'd appreciate it!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload