每次调用方法时,我都需要创建对象的新实例。因此,我需要创建一个循环,以检查是否存在像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];我需要新实例的原因是,我不会覆盖属性值并丢失每个视图的representedObject数据。
发布于 2014-02-18 01:55:31
id object = [[NSClassFromString(@"NameofClass") alloc] init];https://stackoverflow.com/questions/21842872
复制相似问题