Objective c 使用NSString创建类的实例
每次调用一个方法时,我都需要创建一个对象的新实例。因此,我需要创建一个循环来检查变量计数器(如newInstance1)的存在性,如果找到它,则创建newInstance2。如何从nsstring创建类?目标是创建视图,我可以在表示其中对象的网格上拖动这些视图。下面是创建新对象和视图的方法Objective c 使用NSString创建类的实例,objective-c,class,nsstring,Objective C,Class,Nsstring,每次调用一个方法时,我都需要创建一个对象的新实例。因此,我需要创建一个循环来检查变量计数器(如newInstance1)的存在性,如果找到它,则创建newInstance2。如何从nsstring创建类?目标是创建视图,我可以在表示其中对象的网格上拖动这些视图。下面是创建新对象和视图的方法 //method creates view with new fixtureIcon - (IBAction)makeView:(id)sender { rectString = [NSStrin
//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数据 考虑使用NSMutableArray并在其上执行addObject。@Anna我仍然需要以这种方式更改实例的初始化名称。对吗?如果您在问题中解释您试图完成的任务或您试图通过此任务解决的问题,可能会有所帮助。我的意思是:声明一个名为say myInstances的NSMutableArray。创建并向其中添加对象。然后,您可以通过索引来访问它们,例如[myInstances objectAtIndex:2]。@Anna-我正在创建一个对象的新实例,由视图控制器表示。这是动态完成的。用户单击一个按钮来创建新对象。@tyler_nichol。请分享一些代码,它可能会帮助我们了解您正在尝试做什么。从你的评论和问题根本不清楚。考虑使用NStutabReLayx并在AdoTobe上做AdObjor。@安娜,我仍然需要改变那个实例的初始化名称。对吗?如果你在你的问题中解释你想完成什么或者你想解决什么问题,这可能会有帮助。我的意思是:声明一个名为say myInstances的NSMutableArray。创建并向其中添加对象。然后,您可以通过索引来访问它们,例如[myInstances objectAtIndex:2]。@Anna-我正在创建一个对象的新实例,由视图控制器表示。这是动态完成的。用户单击一个按钮来创建新对象。@tyler_nichol。请分享一些代码,它可能会帮助我们了解您正在尝试做什么。从您的评论和问题中根本不清楚。创建类的实例而不是类名本身。比如someclass*stringforclassinstance=[someclass alloc]init];此代码创建类型为NameofClass的类的实例。另外,问题的主题行是:使用NSString创建类的实例。这就是代码的作用。如果你的意思不同,请重新措辞或编辑你的问题。创建类的实例而不是类名本身。比如someclass*stringforclassinstance=[someclass alloc]init];此代码创建类型为NameofClass的类的实例。另外,问题的主题行是:使用NSString创建类的实例。这就是代码的作用。如果你的意思不同,请重新措辞或编辑你的问题。
id object = [[NSClassFromString(@"NameofClass") alloc] init];