每次調用某個方法時,都需要創建一個對象的新實例。所以我需要創建一個循環來檢查像newInstance1這樣的變量計數器的存在,並且如果找到它,則創建newInstance2。你怎麼能從一個nsstring創建一個類?目標是創建視圖,以便可以在表示對象的網格上拖動。下面是創建新的對象,並查看使用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數據爲每個視圖。
考慮使用NSMutableArray並對其做addObject。 – Anna
@Anna我仍然需要改變實例的初始化名稱。右? –
如果你在你的問題中解釋你正在嘗試完成什麼或者你正在試圖解決什麼問題,這可能會有所幫助。我的意思是:聲明一個名爲myInstances的NSMutableArray。創建並添加您的對象。然後你可以通過索引訪問它們(例如'[myInstances objectAtIndex:2]')。 – Anna