custom views

Discussion in 'Mac Programming' started by medasmx, Apr 25, 2009.

  1. medasmx macrumors member

    Joined:
    Nov 9, 2008
    #1
    Enclosed is some code that is supposed to input data to a table view, then load it onto a custom view. I created two classes, TVcontoller and CVcontroller. TVcontroller is an NSObject, CVcontroller a custom view class. There is a textfield, two buttons, a tableview, and a customview. When you type into the textfield and hit enter, it loads it onto the table view. This is done in TVcontroller. The intention was when you press the second button, in CVcontroller, it should load the selecteditem in the table view onto the customview as a string.

    The problem I am having is that the nsmutablearray is not seen in CVcontroller, even though I imported TVcontroller.h and .m. I tried to put the whole project into a zip file but it was 2.3 mb. Below are the two .m files. Thanks for any help.

    Adam

    // TVcontroller.m
    // asmcv3
    //
    // Created by Adam Martin on 4/25/09.
    // Copyright 2009 __MyCompanyName__. All rights reserved.
    //

    #import "TVcontroller.h"


    @implementation TVcontroller

    -(id)init
    {
    [super init];
    myArray=[[NSMutableArray alloc]init];
    [tableView setDelegate:self];
    return self;
    }

    -(IBAction)button1:(id)sender
    {
    myString=[myTextField stringValue];
    [myTextField setStringValue:mad:""];
    [myArray addObject:myString];
    [tableView reloadData];
    }

    -(int)numberOfRowsInTableView:(NSTableView *)tv
    {
    return [myArray count];
    }

    -(id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row
    {
    return [myArray objectAtIndex:row];
    }

    @end

    // CVcontroller.m
    // asmcv3
    //
    // Created by Adam Martin on 4/25/09.
    // Copyright 2009 __MyCompanyName__. All rights reserved.
    //

    #import "CVcontroller.h"
    #import "TVcontroller.m"
    #import "TVcontroller.h"

    @implementation CVcontroller

    - (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
    // Initialization code here.
    }
    return self;
    }

    - (void)drawRect:(NSRect)rect {
    myPath=[NSBezierPath bezierPathWithRect:rect];
    NSColor*myColor=[NSColor blueColor];
    [myColor set];
    [myPath fill];
    myPointA=NSMakePoint((rect.size.width/2-25),(rect.size.height/2-25));
    myPointB=NSMakePoint((rect.size.width/2-25)+50,(rect.size.height/2-25));
    myPointC=NSMakePoint((rect.size.width/2-25)+50,(rect.size.height/2-25)+50);
    myTriangle=[NSBezierPath bezierPath];
    [myTriangle moveToPoint:myPointA];
    [myTriangle lineToPoint:myPointB];
    [myTriangle lineToPoint:myPointC];
    [myTriangle closePath];
    [[NSColor yellowColor]set];
    [myTriangle fill];
    }

    -(IBAction)button2:(id)sender
    {
    myString3=[myArray objectAtIndex:[tableView selectedRow]];
    myPointD=NSMakePoint(0,0);
    [myString3 drawAtPoint:myPointD withAttributes:nil];
    }

    @end
     
  2. medasmx thread starter macrumors member

    Joined:
    Nov 9, 2008
    #2
    zip file

    If anyone wants the zip file, I can email it. Thanks.

    Adam
     
  3. medasmx thread starter macrumors member

    Joined:
    Nov 9, 2008
    #3
    custom views2

    I solved the problem(refer to prior thread custom view). Below is the code. Basically the idea is to put items from a textfield into a tableview, then click and load them onto a cutomview. I have been trying over the past several weeks to learn about customviews, with this being an exercise to work on that. I would be happy to email the zip file.

    Adam

    // ftnstablevasm.m
    // firsttrynstablev
    //
    // Created by Adam Martin on 12/21/08.
    // Copyright 2008 __MyCompanyName__. All rights reserved.
    //

    #import "ftnstablevasm.h"
    #import "CVcontroller.h"

    @implementation ftnstablevasm

    -(id)init
    {
    [super init];
    myArray=[[NSMutableArray alloc]init];
    [tableView setDelegate:self];
    return self;
    }
    -(IBAction)asmbutton1:(NSButton*)sender;
    {
    myInputString=[input1 stringValue];
    [input1 setStringValue:mad:""];
    [myArray addObject:myInputString];
    [tableView reloadData];
    }

    -(int)numberOfRowsInTableView:(NSTableView *)tv
    {
    return [myArray count];
    }

    -(id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row
    {
    return [myArray objectAtIndex:row];
    }

    -(IBAction)asmbutton2:(NSButton*)sender;
    {
    mySecondString=[myArray objectAtIndex:[tableView selectedRow]];
    [CVoutlet setString:mySecondString];
    }

    // CVcontroller.m
    // firsttrynstablev
    //
    // Created by Adam Martin on 4/26/09.
    // Copyright 2009 __MyCompanyName__. All rights reserved.
    //

    #import "CVcontroller.h"


    @implementation CVcontroller

    -(void)prepareAttributes
    {
    attributes=[[NSMutableDictionary alloc]init];
    [attributes setObject:[NSFont fontWithName:mad:"Helvetica" size:10] forKey:NSFontAttributeName];
    [attributes setObject:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
    }

    - (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (!self) return nil;
    [self prepareAttributes];
    string=@"";
    return self;
    }

    -(void)setString:(NSString*)c
    {
    c=[c copy];
    [string release];
    string=c;
    [self setNeedsDisplay:YES];
    }

    -(void)drawStringCenteredIn:(NSRect)rect
    {
    [string drawAtPoint:NSMakePoint(0,0) withAttributes:attributes];
    }

    - (void)drawRect:(NSRect)rect {
    myPath=[NSBezierPath bezierPathWithRect:rect];
    NSColor*myColor=[NSColor blueColor];
    [myColor set];
    [myPath fill];
    myPointA=NSMakePoint((rect.size.width/2-25),(rect.size.height/2-25));
    myPointB=NSMakePoint((rect.size.width/2-25)+50,(rect.size.height/2-25));
    myPointC=NSMakePoint((rect.size.width/2-25)+50,(rect.size.height/2-25)+50);
    myTriangle=[NSBezierPath bezierPath];
    [myTriangle moveToPoint:myPointA];
    [myTriangle lineToPoint:myPointB];
    [myTriangle lineToPoint:myPointC];
    [myTriangle closePath];
    [[NSColor yellowColor]set];
    [myTriangle fill];
    [self drawStringCenteredIn:rect];
    }
    @end
     
  4. medasmx thread starter macrumors member

    Joined:
    Nov 9, 2008
    #4
    custom views3

    The follow-up question to my last two posts, is -- how do I create a matrix of custom views. I tried putting two custom views on the nib, then using layout to embed them in a matrix. That doesn't seem to work. Thanks for any help.

    Adam
     
  5. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #5
    use [ code] and [/ code] (without the spaces) to enclose your code. It helps readability.
     

Share This Page