首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用NSString创建类实例

用NSString创建类实例
EN

Stack Overflow用户
提问于 2014-02-18 01:51:47
回答 1查看 428关注 0票数 1

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

代码语言:javascript
复制
//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];

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-18 01:55:31

代码语言:javascript
复制
id object = [[NSClassFromString(@"NameofClass") alloc] init];
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21842872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档