How to add a button in ImageKitDemo samplecode?

Discussion in 'Mac Programming' started by cea, Nov 9, 2011.

  1. cea macrumors newbie

    Joined:
    Oct 26, 2009
    #1
  2. cea thread starter macrumors newbie

    Joined:
    Oct 26, 2009
    #2
    In the ImageBrowserCell.m, i tried to put the NSButton in a NSView container, then put it in a CALayer. The buttons appear as expected in each IKImageBrowerCell but they are not functional:

    ImageBrowserCell.m:
    PHP:
    - (CALayer *) layerForType:(NSString*) type
    {
    --- 
    snips ---
            
    //add a glossy overlay
            
    CALayer *glossyLayer = [CALayer layer];
            
    glossyLayer.frame = *(CGRect*) &relativeImageContainerFrame;
            
            
    NSView *container = [[[NSView allocinitWithFrame:NSMakeRect(0,0,25,25)] autorelease];
            
    NSButton *button = [[[NSButton allocinitWithFrame:NSMakeRect(0,0,25,25)] autorelease];
            [
    container addSubview:button];
            [
    button setImage:[NSImage imageNamed:@"pin.tiff"]];
            [
    button setTitle:@"BUTTON"];
            
            [
    container setLayer:layer];
            [
    container setWantsLayer:YES];
            [
    container addSubview:button];
            
            [
    glossyLayer setContents:container];
            [
    layer addSublayer:glossyLayer];

    --- 
    snip ---
    }
    Can anyone please help me?
     
  3. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #3
    Have you considered setting the button's action?
     
  4. cea thread starter macrumors newbie

    Joined:
    Oct 26, 2009
    #4
    This is the issue. The IKImageBrowserCell creates the cells dynamically using CALayer code above, the cells are not even appear in the nib, so i cannnot set IBAction for buttons in the nib.
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    No one suggested that.

    See the setAction: and setTarget: methods of the NSControl class. Since NSButton is a subclass of NSControl, it has those methods.
     
  6. cea thread starter macrumors newbie

    Joined:
    Oct 26, 2009
    #6
    I added the setAction: and setTarget: methods and the Tooltip.
    When i hold the mouse over the button for test, the tooltip for the container does not appear as expected, the same with the button. Click on the button does not happen anything.

    ImageBrowserCell.m:
    PHP:
    - (CALayer *) layerForType:(NSString*) type
    {
    --- 
    snips ---
            
    //add a glossy overlay
            
    CALayer *glossyLayer = [CALayer layer];
            
    glossyLayer.frame = *(CGRect*) &relativeImageContainerFrame;
            
            
    NSView *container = [[[NSView allocinitWithFrame:NSMakeRect(0,0,25,25)] autorelease];
            
    NSButton *button = [[[NSButton allocinitWithFrame:NSMakeRect(0,0,25,25)] autorelease];
            [
    container addSubview:button];
            [
    button setImage:[NSImage imageNamed:@"pin.tiff"]];
            [
    button setTitle:@"BUTTON"];
    [
    button setAction:@selector(deleteMe:)];
     [
    button setTarget:self];
     
    //[button setToolTip:@"This is the button tooltip test."];
    [container setToolTip:@"This is the container tooltip test."];
            
            [
    container setLayer:layer];
            [
    container setWantsLayer:YES];
            [
    container addSubview:button];
            
            [
    glossyLayer setContents:container];
            [
    layer addSublayer:glossyLayer];

    --- 
    snip ---
    }

    -(
    IBAction)deleteMe:(id)sender {
        
    NSLog(@"This is deleteMe.");
    }
     

Share This Page