使用NSString创建类的实例

时间:2014-02-18 01:51:48

标签: objective-c class nsstring

每次调用方法时,我都需要创建一个对象的新实例。所以我需要创建一个循环,检查是否存在变量计数器,如newInstance1,如果找到则创建newInstance2。如何从nsstring创建一个类?目标是创建视图,我可以在已在其中表示对象的网格上拖动。这是创建新对象和视图的方法

//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];

我需要新实例的原因是我不会覆盖属性值并丢失每个视图的representObject数据。

1 个答案:

答案 0 :(得分:4)

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