tyler_nichol
tyler_nichol

Reputation: 33

create instance of class with NSString

I need to create a new instance of an object every time a method is called. So I need to create a loop that checks for the existence of a variable counter like newInstance1 and if it is found create newInstance2. How can you create a class from an nsstring? The goal is to create views that I can drag around on a grid that have represented objects in them. Here is that method that creates the new object and view

//method creates view with new fixtureIcon
- (IBAction)makeView:(id)sender {


    rectString = [NSString stringWithFormat:@"0,120,%f,%f", pm.fixWidth, pm.fixHeight];
    _try = [[fixtureIcon alloc]init];

    _try.fixImg = pm.fixImage;
    _try.name = pm.name;
    [fixController addObject:_try];     




    _testBox = [[addObjectView alloc]initWithFrame:NSRectFromString(rectString)];



    NSImageView *iconView = [[NSImageView alloc]initWithFrame:NSMakeRect(0, 0, 75, 75)];
    testView1Controller = [[NSViewController alloc]init];
    testView1Controller.view = _testBox;


    [_testBox setWantsLayer:YES];




    _testBox.layer.masksToBounds = YES;
    _testBox.layer.cornerRadius = 2.0;
    _testBox.layer.borderWidth = 2;
    _testBox.layer.borderColor = [[NSColor darkGrayColor]CGColor];

    //this tells the window to add our subview testbox to it's contentview

    [testView1Controller setView:_testBox];

    [testView1Controller bind:@"representedObject" toObject:fixController withKeyPath:@"content" options:nil];

    [mainWin addSubview:_testBox];
    NSTextField *obLabel = [[NSTextField alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
    [obLabel bind:@"value" toObject:testView1Controller withKeyPath:@"representedObject.type" options:nil];
    [obLabel setBackgroundColor:[NSColor clearColor]];
    [obLabel setSelectable:false];
    [obLabel setEditable:false];
    [obLabel setBackgroundColor:[NSColor grayColor]];
    [obLabel setBordered:false];
    [obLabel setTextColor:[NSColor whiteColor]];
    [iconView bind:@"value" toObject:testView1Controller withKeyPath:@"representedObject.fixImg" options:nil];
    //[_testBox addSubview:obLabel];
    [_testBox addSubview:iconView];

The reason I need new instances is so I don't overwrite the property values and lose my representedObject data for each view.

Upvotes: 1

Views: 441

Answers (1)

Aaron
Aaron

Reputation: 7145

id object = [[NSClassFromString(@"NameofClass") alloc] init];

Upvotes: 4

Related Questions